Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS指令未注入&;给出错误_Angularjs_Angularjs Directive - Fatal编程技术网

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>的现有模块,如果之前没有创建该模块,则会引发异常。

我同意,这是所有事件都很常见吗(包括
控制器
)不确定你在问什么…或者这是否是一个问题我同意,这是常见的所有事件所有(包括
控制器
)不确定你在问什么…或者这是否是一个问题