<!DOCTYPE html>
<html>
<head>
<title>Angullr</title>
</head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp">
<div ng-controller="newController">
{{hello}}
<div my-direcitve></div>
</div>
</div>
</body>
<script type="text/javascript">
(function() {
dir = angular.module('mydirective',[]);
dir.directive('myDirecitve', function() {
return {
template: '<ul><li>No Hello</li></ul>'
};
});
});
var app = angular.module('myApp',['mydirective']);
app.controller('newController',function($scope,myDirecitve){
$scope.hello = 'hello Wolrd';
});
</script>
</html>
由于以下原因导致无法实例化myApp模块: 错误:[$ injector:modulerr] http://errors.angularjs.org/1.4.8/ $ injector / modulerr?p0 = m ...) 在错误(本机)
答案 0 :(得分:1)
您的指令应使用相同的模块,从'mydirective'
更改为'myApp'
(function() {
dir = angular.module('myApp');
dir.directive('myDirecitve', function() {
return {
template: '<ul><li>No Hello</li></ul>'
};
});
});
答案 1 :(得分:0)
您的代码完全不匹配,为什么您使用了多个模块?你在巡演HTMl中称之为'mydirective'模块的地方?这有什么用?
您的代码应如下所示
<script type="text/javascript">
var app = angular.module('myApp',[]);
app.directive('myDirecitve', function() {
return {
template: '<ul><li>No Hello</li></ul>'
};
});
app.controller('newController',function($scope,myDirecitve){
$scope.hello = 'hello Wolrd';
});
</script>
答案 2 :(得分:0)
首先,您不需要向控制器添加指令
秒,有两种方法可以尝试:
1 使用指令作为模块:
<script type="text/javascript">
var dir = angular.module('mydirective',[]);
dir.directive('myDirective', function() {
return {
template: '<ul><li>No Hello</li></ul>'
};
});
var app = angular.module('myApp',['mydirective']);
app.controller('newController',['$scope',function($scope){
$scope.hello = 'hello World';
}]);
</script>
在myApp模块中使用2 :
<script type="text/javascript">
var app = angular.module('myApp',[]);
app.controller('newController',['$scope',function($scope){
$scope.hello = 'hello World';
}]);
app.directive('myDirective', function() {
return {
template: '<ul><li>No Hello</li></ul>'
};
});
</script>
我测试了它的确有效,希望这有帮助!