2016-09-07 7 views
0

私は新しいサービスをコントローラに注入された場合、これをフォーマットしたがためにいくつかの方法があります知っている:ディレクティブはサービスを一覧表示する必要がありますか?

AnalyticsController.$inject = ['$scope', 'analyticsService', 'nvd3', 'gridster']; 

サービスはまだ角1.5でディレクティブにする必要がありますか?

import { AnalyticsController } from './analytics.controller'; 

export class AnalyticsComponent { 

    constructor(){ 
    this.bindings = { 
     chartData: '<' 
    }; 
    this.controller = AnalyticsController; 
    this.controllerAs = 'vm'; 
    this.templateUrl = 'analytics/analytics.html'; 
//Maybe service? 
    } 
} 
+0

角度2で意味しますか? –

+0

角1.5(2に向かって移動しようとしていますが、まだ使用していません)。 @ d3l33t答えが意味をなす、私は自分自身を明確にするためにhttps://docs.angularjs.org/guide/componentを読んでいる。 – cdb

答えて

1

サービスをインジェクトした後、サービスにクラスを公開するためにコンストラクタを使用します。

AnalyticsComponent.$inject = ['$scope', 'analyticsService', 'nvd3', 'gridster']; 

export class AnalyticsComponent { 
    constructor($scope, analyticsService, nvd3, gridster) { 
     this.bindings = { 
      chartData: '<' 
     }; 
     this.controller = AnalyticsController; 
     this.controllerAs = 'vm'; 
     this.templateUrl = 'analytics/analytics.html'; 
     this.analyticsService = analyticsService; 
    } 

    setAnalytics(data) { 
     this.analyticsService.methodName(data); 
    } 
} 
関連する問題