2017-10-17 3 views
0

각도 멍청합니다.지시문에 공장 주입이 작동하지 않습니다.

내 공장 (beerFactory)을 지시문에 삽입하려고하지만 링크 기능에서 액세스 할 수 없습니다.

내 코드에서 볼 수 있듯이 일단 링크 기능 내에 있으면 일단 beerFactory 객체를 로깅하려고합니다.

공장이 다른 컨트롤러 내부에서 사용 중이므로 공장이 올바르게 작동하고 있습니다.

또한 인수로 전달해야 내가

답변

0

잘못

app.directive('starRating', ['beerFactory', function(){ 
 
    return { 
 
       restrict: 'EA', 
 
       scope: { 
 
        'value': '=value', 
 
        'max': '=max', 
 
        'hover': '=hover', 
 
        'isReadonly': '=isReadonly' 
 
       }, 
 
       link: function(scope, element, attrs, ctrl) { 
 
        
 
       console.log(beerFactory); 
 

 
         function renderValue() { 
 
          scope.renderAry = []; 
 
          for (var i = 0; i < scope.max; i++) { 
 
           if (i < scope.value) { 
 
            scope.renderAry.push({ 
 
             'fa fa-star fa-2x': true 
 
            }); 
 
           } else { 
 
            scope.renderAry.push({ 
 
             'fa fa-star-o fa-2x': true 
 
            }); 
 
           } 
 
          } 
 
         } 
 
        
 
         scope.setValue = function (index) { 
 
          if (!scope.isReadonly && scope.isReadonly !== undefined) { 
 
           scope.value = index + 1; 
 
          } 
 
         }; 
 
        
 
         scope.changeValue = function (index) { 
 
          if (scope.hover) { 
 
           scope.setValue(index); 
 
          } else { 
 
           // !scope.changeOnhover && scope.changeOnhover != undefined 
 
          } 
 
         }; 
 
        
 
         scope.$watch('value', function (newValue, oldValue) { 
 
          if (newValue) { 
 
           scope.updateRating(newValue); 
 
           renderValue(); 
 
          } 
 
         }); 
 
         scope.$watch('max', function (newValue, oldValue) { 
 
          if (newValue) { 
 
           renderValue(); 
 
          } 
 
         }); 
 
        
 
        }, 
 
       template: '<span ng-class="{isReadonly: isReadonly}">' + 
 
        '<i ng-class="renderObj" ' + 
 
        'ng-repeat="renderObj in renderAry" ' + 
 
        'ng-click="setValue($index)" ' + 
 
        'ng-mouseenter="changeValue($index, changeOnHover)" >' + 
 
        '</i>' + 
 
        '</span>', 
 
       replace: true 
 
      }; 
 
}]);
을하고있어 무엇 아무 생각이 없습니다 :

app.directive('starRating', ['beerFactory', function(beerFactory){ 

https://docs.angularjs.org/guide/di보기 -> 인라인 배열 주석

이 주석 유형을 사용하는 경우 주석 배열을 함수 선언의 매개 변수와 동기화 상태로 유지해야합니다.

+0

감사합니다. –

관련 문제