I am using strict mode and Angular 1.4.7
, I get the following error:
我使用的是严格模式和角1。4.7,我得到如下错误:
Error: [$injector:strictdi] function($scope, $element, $attrs, mouseCapture) is not using explicit annotation and cannot be invoked in strict mode
The angular generated url for the error is:
误差产生的角度url为:
https://docs.angularjs.org/error/$injector/strictdi?p0=function($scope,%20$element,%20$attrs,%20mouseCapture
https://docs.angularjs.org/error/ $喷射器/ strictdi ? p0 =函数(范围、美元% 20美元的元素,% 20 $ attrs % 20 mousecapture
And the following is the service
以下是服务
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);
},
};
})
;
How do i fix this error
如何修正这个错误
2 个解决方案
#1
35
From the documentation it looks like you need to declare all dependency injections in string array.
从文档来看,似乎需要在字符串数组中声明所有依赖项注入。
There are other ways but normally I would do it like this:
还有其他的方法,但通常我会这样做:
controller: ['$scope', '$element', '$attrs', 'mouseCapture',
function($scope, $element, $attrs, mouseCapture) {
...
}
]
One of the reason we do this is because when we try to minify this js file, variable names would be reduced to one or 2 characters, and DI needs the exact name to find the services. By declaring DI in a string array, angular can match services with their minified variable name. For this reason, the string array and the function arguments need EXACT MATCHING in number and order.
我们这样做的原因之一是,当我们试图缩小这个js文件时,变量名将减少到一个或两个字符,而DI需要确切的名称来查找服务。通过在字符串数组中声明DI, angle可以将服务与其缩小的变量名匹配。因此,字符串数组和函数参数需要在数量和顺序上进行精确匹配。
Update:
更新:
If you are following John Papa's Angular style guide, you should do it like this:
如果你遵循约翰·爸爸的棱角分明的风格指南,你应该这样做:
controller: MouseCaptureController,
...
MouseCaptureController.$inject = ['$scope', '$element', '$attrs', 'mouseCapture'];
function MouseCaptureController($scope, $element, $attrs, mouseCapture) {
...
}
#2
8
The code says:
的代码表示:
$injector:strictdi
$注射器:strictdi
There is an error with the dependency injection In the documentation for this error:
在文档中有一个关于这个错误的依赖注入的错误:
Strict mode throws an error whenever a service tries to use implicit annotations
当服务试图使用隐式注解时,严格模式会抛出错误
You should try to switching to:
你应尝试转到:
.factory('mouseCapture', ['$rootScope', function ($rootScope) {...}]);
syntax, whenever in strict mode.
语法,在严格模式下。
#1
35
From the documentation it looks like you need to declare all dependency injections in string array.
从文档来看,似乎需要在字符串数组中声明所有依赖项注入。
There are other ways but normally I would do it like this:
还有其他的方法,但通常我会这样做:
controller: ['$scope', '$element', '$attrs', 'mouseCapture',
function($scope, $element, $attrs, mouseCapture) {
...
}
]
One of the reason we do this is because when we try to minify this js file, variable names would be reduced to one or 2 characters, and DI needs the exact name to find the services. By declaring DI in a string array, angular can match services with their minified variable name. For this reason, the string array and the function arguments need EXACT MATCHING in number and order.
我们这样做的原因之一是,当我们试图缩小这个js文件时,变量名将减少到一个或两个字符,而DI需要确切的名称来查找服务。通过在字符串数组中声明DI, angle可以将服务与其缩小的变量名匹配。因此,字符串数组和函数参数需要在数量和顺序上进行精确匹配。
Update:
更新:
If you are following John Papa's Angular style guide, you should do it like this:
如果你遵循约翰·爸爸的棱角分明的风格指南,你应该这样做:
controller: MouseCaptureController,
...
MouseCaptureController.$inject = ['$scope', '$element', '$attrs', 'mouseCapture'];
function MouseCaptureController($scope, $element, $attrs, mouseCapture) {
...
}
#2
8
The code says:
的代码表示:
$injector:strictdi
$注射器:strictdi
There is an error with the dependency injection In the documentation for this error:
在文档中有一个关于这个错误的依赖注入的错误:
Strict mode throws an error whenever a service tries to use implicit annotations
当服务试图使用隐式注解时,严格模式会抛出错误
You should try to switching to:
你应尝试转到:
.factory('mouseCapture', ['$rootScope', function ($rootScope) {...}]);
syntax, whenever in strict mode.
语法,在严格模式下。