angular $parse 作为指令参数爆炸了

angular $parse is blowing up as a directive parameter

我正在尝试按照这个对旧问题的回答进行操作,但我在将 $parse 作为函数参数时遇到了崩溃。为什么它不能识别 $parse?

.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);
                        })
                    })
                }
            }
        });

您需要以字符串格式指定注入。这仅在丑化您的源代码时才需要。

方法 1:声明 angular 注入时使用数组格式

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

方法二:使用命名函数并手动指定注入

.directive('elementReady', elementReady)

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

方法 3:使用 ng-annotation 自动执行此操作

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

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