2017-01-28 23 views
0

は、私はそれはそれはAPIからのを必要とし、任意の場所に注入されるデータを収集することができますので、それは自分のコントローラのしているディレクティブを開発しようとしています。角度指令コントローラスコープ

は、ここで私はこれまで得たものです:

(function() { 
'use strict'; 

angular 
    .module('app.hostelsmg.bookings') 
    .directive('bookingsChart', bookingsChart); 

function bookingsChart() { 
    return { 
     restrict: 'E', 
     scope: true, 
     controller: [function(){ 
      $scope.test = 'Hi there!'; 
     }], 
     compile: function(tElem, attrs){ 
      tElem.html('{{test}}'); 
      return function(scope, elems, attrs){ 

      } 
     } 
    } 
} 
})(); 

だから私が好きなものを、それ自身のコントローラからデータを取得するためのディレクティブです。これまでのところ、私はそれをそのように働かせることはできませんでした。

どのように知っていますか?

+0

ようなものを使用することができます。あなたのコントローラに '$ scope'を注入して、あなたのテンプレートに' $ scope'に割り当てられた変数を直接使用してください。 – 31piy

答えて

1

あなたはあなたのコードが正常に動作する必要があり、この

function bookingsChart() { 
    return { 
     restrict: 'E', 
     scope: true, 
     controller: ['$scope', 'yourservice', function ($scope, yourservice) { 
      $scope.data = []; //bind this data to your view 

      $scope.getServiceData = function (count) { 
       //your service call 
       yourservice.getServiceData(count).then(function (data) { 
        $scope.data = data; //sets data in $scope.data variable 
       }); 
      } 
     }], 
     link: function (scope, elements, attributes) { 
      //if you want to pass any parameters from your UI to your service method 
      scope.getServiceData(attributes.count); //calls the getServiceData in controller defined above 
     } 
    } 
}