2016-12-06 16 views
3

我想按照這個答案舊的問題,但我得到一個崩潰的$ parse作爲函數參數。爲什麼它不能識別$ parse?

https://stackoverflow.com/a/29571230/1481314

.directive('elementReady', function ($parse) { 
      return { 
       restrict: 'A', 
       link: function ($scope, elem, attrs) { 
        elem.ready(function() { 
         $scope.$apply(function() { 
          var func = $parse(attrs.elementReady); 
          func($scope); 
         }) 
        }) 
       } 
      } 
     }); 
+0

你得到的錯誤是什麼?你有沒有試過使用'''['$ parse',function($ parse){...}]'''來指定它作爲數組格式的依賴項? –

+0

你是否縮小了腳本? – devqon

+0

像@devqon說的,如果你縮小了腳本,並沒有放置依賴或將它注入其他地方,它將失敗 – ocespedes

回答

0

你需要指定字符串格式的注射。這隻有在你使用源代碼的時候才需要。

方法1:使用陣列形式宣佈角注射

.directive('elementReady', ['$parse' function elementReady($parse) { 
    ... 
}]); 

方法2時:命名的函數的使用和手動指定注射

.directive('elementReady', elementReady) 

function elementReady($parse) { 
    ... 
} 
elementReady.$inject = ['$parse']; 

方法3:使用NG-註釋自動執行此

GRUNT:https://www.npmjs.com/package/grunt-ng-annotate

GULP:https://www.npmjs.com/package/gulp-ng-annotate/