AngularJS指令未注入&;给出错误
我正在尝试注入一个AngularJS指令未注入&;给出错误,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我正在尝试注入一个指令,但出现了一些错误。实际上,我已经将模块声明为数组,并将其推送到应用程序,但仍然无法工作 这是我的密码: var appModules = ['app.directives', []] var app = angular.module('plunker', appModules); app.controller('MainCtrl', function($scope) { $scope.name = 'World'; }); angular.module('a
指令
,但出现了一些错误。实际上,我已经将模块
声明为数组,并将其推送到应用程序,但仍然无法工作
这是我的密码:
var appModules = ['app.directives', []]
var app = angular.module('plunker', appModules);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
});
angular.module('app.directives', []).directive('myContent', function(){
return{
link:function(scope, element, attrs){
console.log("hi");
}
}
})
模块声明的第二个参数是依赖项数组。但是在要传递给injection的AppModule中有一个额外的空数组 改变
var appModules = ['app.directives', []]
到
模块声明的第二个参数是依赖项数组。但是在要传递给injection的AppModule中有一个额外的空数组 改变
var appModules = ['app.directives', []]
到
将
var-appModules=['app.directives',[]]
更改为var-appModules=['app.directives']将var-appModules=['app.directives',[]
更改为var-appModules=['app.directives']
在appModules
中不能使用空白数组
var appModules = ['app.directives'];
因为它不会识别模块,所以里面有一个空数组
var app = angular.module('plunker', ['app.directives', []]);
当我们直接注入appModules
时,它显然会给出一个错误
未能实例化模块
因为它无法识别其中的空数组
var appModules = ['app.directives'];
这就是您必须使用的。在appModules
中,您不能在其中使用空白数组
var appModules = ['app.directives'];
因为它不会识别模块,所以里面有一个空数组
var app = angular.module('plunker', ['app.directives', []]);
当我们直接注入appModules
时,它显然会给出一个错误
未能实例化模块
因为它无法识别其中的空数组
var appModules = ['app.directives'];
这就是您必须使用的功能。按以下方式更改代码:
// Define the name of the directive module
var directiveModule = 'app.directives';
// Create the directive module itself
angular.module('app.directives', []);
var appModules = [directiveModule];
// Create the main module
var app = angular.module('plunker', appModules);
// Add controller on the main module
app.controller('MainCtrl', function ($scope) {
$scope.name = 'World';
});
// Register directive on the directives module
angular.module('app.directives').directive('myContent', function () {
return {
link: function (scope, element, attrs) {
console.log("hi");
}
}
});
请记住:angular.module('some-name',[])
创建一个名为some-name
的新模块,而angular.module('some-name')
将使用名为some-name
的现有模块,如果之前未创建该模块,则会引发异常。按以下方式更改代码:
// Define the name of the directive module
var directiveModule = 'app.directives';
// Create the directive module itself
angular.module('app.directives', []);
var appModules = [directiveModule];
// Create the main module
var app = angular.module('plunker', appModules);
// Add controller on the main module
app.controller('MainCtrl', function ($scope) {
$scope.name = 'World';
});
// Register directive on the directives module
angular.module('app.directives').directive('myContent', function () {
return {
link: function (scope, element, attrs) {
console.log("hi");
}
}
});
记住:angular.module('some-name',[])
创建一个名为some-name
的新模块,而angular.module('some-name')
将使用名为some-name>的现有模块,如果之前没有创建该模块,则会引发异常。我同意,这是所有事件都很常见吗(包括控制器
)不确定你在问什么…或者这是否是一个问题我同意,这是常见的所有事件所有(包括控制器
)不确定你在问什么…或者这是否是一个问题