gpt4 book ai didi

javascript - 如何修复 Angular 不使用显式注释并且不能在严格模式下调用

转载 作者:IT王子 更新时间:2023-10-29 03:14:41 32 4
gpt4 key购买 nike

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

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

错误的 Angular 生成的 url 是:

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

},
};
})
;

如何修复这个错误

最佳答案

来自documentation看起来您需要在字符串数组中声明所有依赖项注入(inject)。

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

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

关于javascript - 如何修复 Angular 不使用显式注释并且不能在严格模式下调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33383854/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com