如何修复Angular不使用显式注释,不能在严格模式下调用

时间:2015-10-28 05:55:06

标签: javascript angularjs angularjs-directive

我正在使用严格模式和Angular 1.4.7,我收到以下错误:

Error: [$injector:strictdi] function($scope, $element, $attrs, mouseCapture) is not using explicit annotation and cannot be invoked in strict mode

错误的角度生成网址为:

https://docs.angularjs.org/error/ $注射器/ strictdi?P0 =函数($范围,%20 $元件,%20 $ ATTRS,%20mouseCapture

以下是服务

angular.module('mouseCapture', [])

//
// Service used to acquire 'mouse capture' then receive dragging events while the mouse is captured.
//
.factory('mouseCapture', function ($rootScope) {

    //
    // Element that the mouse capture applies to, defaults to 'document' 
    // unless the 'mouse-capture' directive is used.
    //
    var $element = document; 

    //
    // Set when mouse capture is acquired to an object that contains 
    // handlers for 'mousemove' and 'mouseup' events.
    //
    var mouseCaptureConfig = null;

    //
    // Handler for mousemove events while the mouse is 'captured'.
    //
    var mouseMove = function (evt) {

        if (mouseCaptureConfig && mouseCaptureConfig.mouseMove) {

            mouseCaptureConfig.mouseMove(evt);

            $rootScope.$digest();
        }
    };

    //
    // Handler for mouseup event while the mouse is 'captured'.
    //
    var mouseUp = function (evt) {

        if (mouseCaptureConfig && mouseCaptureConfig.mouseUp) {

            mouseCaptureConfig.mouseUp(evt);

            $rootScope.$digest();
        }
    };

    return {

        // 
        // Register an element to use as the mouse capture element instead of 
        // the default which is the document.
        //
        registerElement: function(element) {

            $element = element;
        },

        //
        // Acquire the 'mouse capture'.
        // After acquiring the mouse capture mousemove and mouseup events will be 
        // forwarded to callbacks in 'config'.
        //
        acquire: function (evt, config) {

            //
            // Release any prior mouse capture.
            //
            this.release();

            mouseCaptureConfig = config;

            // 
            // In response to the mousedown event register handlers for mousemove and mouseup 
            // during 'mouse capture'.
            //
            $element.mousemove(mouseMove);
            $element.mouseup(mouseUp);
        },

        //
        // Release the 'mouse capture'.
        //
        release: function () {

            if (mouseCaptureConfig) {

                if (mouseCaptureConfig.released) {
                    //
                    // Let the client know that their 'mouse capture' has been released.
                    //
                    mouseCaptureConfig.released();
                }

                mouseCaptureConfig = null;
            }

            $element.unbind("mousemove", mouseMove);
            $element.unbind("mouseup", mouseUp);
        },
    };
})

//
// Directive that marks the mouse capture element.
//
.directive('mouseCapture', function () {
  return {
    restrict: 'A',

    controller: function($scope, $element, $attrs, mouseCapture) {

        // 
        // Register the directives element as the mouse capture element.
        //
        mouseCapture.registerElement($element);

    },
  };
})
;

如何修复此错误

4 个答案:

答案 0 :(得分:43)

documentation看起来你需要在字符串数组中声明所有依赖注入。

还有其他方法,但通常我会这样做:

controller: ['$scope', '$element', '$attrs', 'mouseCapture',
  function($scope, $element, $attrs, mouseCapture) {
    ...
  }
]

我们这样做的原因之一是因为当我们尝试缩小此js文件时,变量名称将减少为一个或两个字符,并且DI需要确切的名称来查找服务。通过在字符串数组中声明DI,angular可以将服务与其缩小的变量名称匹配。因此,字符串数组和函数参数需要数量和顺序的精确匹配。

更新

如果你关注John Papa's Angular style guide,你应该这样做:

controller: MouseCaptureController,
...

MouseCaptureController.$inject = ['$scope', '$element', '$attrs', 'mouseCapture'];

function MouseCaptureController($scope, $element, $attrs, mouseCapture) {
  ...
}

答案 1 :(得分:8)

代码说:

  

$注射器:strictdi

依赖注入存在错误 在此错误的文档中:

  

只要服务尝试使用隐式注释,严格模式就会抛出错误

你应该尝试切换到:

.factory('mouseCapture', ['$rootScope', function ($rootScope) {...}]);

语法,无论何时处于严格模式。

答案 2 :(得分:0)

只需将'ngInject'添加到构造函数的第一行即可。

答案 3 :(得分:0)

添加“ ngInject”是对我有用的答案。实际上,我正在将typescript与angularjs一起使用,并在构造函数完成此操作之前添加/ @ngInject /。