2017-01-28 4 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 
     } 
    } 
} 
관련 문제