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

Mr *_*xin 34 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
Run Code Online (Sandbox Code Playgroud)

角度生成的错误网址是:

https://docs.angularjs.org/error/ $ injector/strictdi?p0 = function($ scope,%20 $ element,%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);

    },
  };
})
;
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个错误

Icy*_*ool 43

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

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

controller: ['$scope', '$element', '$attrs', 'mouseCapture',
  function($scope, $element, $attrs, mouseCapture) {
    ...
  }
]
Run Code Online (Sandbox Code Playgroud)

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


更新:

如果您正在关注John Papa的Angular风格指南,您应该这样做:

controller: MouseCaptureController,
...

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

function MouseCaptureController($scope, $element, $attrs, mouseCapture) {
  ...
}
Run Code Online (Sandbox Code Playgroud)

  • 一个问题,如何找到哪个服务是罪魁祸首?在我的场景中,Angular显示了一个只用角度方法调用填充的调用堆栈.我找不到*什么服务*. (4认同)

小智 8

代码说:

$喷油器:strictdi

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

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

您应该尝试切换到:

.factory('mouseCapture', ['$rootScope', function ($rootScope) {...}]);
Run Code Online (Sandbox Code Playgroud)

语法,只要在严格模式下.