在下面的示例中,有三个模块,第一个模块需要第一个模块。这在使用ng-app
中的div
引用时有效。但是名为someonesApp
的第三个模块不起作用。使用ng-app引用第二个模块时,不运行run方法。没有前两个模块第三个工作,为什么会这样?
<!DOCTYPE html>
<html >
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
</head>
<body ng-init="name='123'">
<div ng-app="myApp">
</div>
<div ng-app="someonesApp">
</div>
<script>
var yourApp = angular.module('yourApp',[]);
yourApp.run(function(){
console.log('run block of yourApp');
});
var myApp = angular.module('myApp',['yourApp']);
myApp.run(function(){
console.log('run block of myApp');
});
var someonesApp = angular.module('someonesApp',[]);
someonesApp.run(function(){
console.log('run block of someonesApp');
});
</script>
</body>
</html>
以下是修改过的代码,在这种情况下,我删除了<div ng-app="myApp"></div>
,第三个模块正常工作。
<!DOCTYPE html>
<html >
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
</head>
<body ng-init="name='123'">
<div ng-app="someonesApp">
</div>
<script>
var yourApp = angular.module('yourApp',[]);
yourApp.run(function(){
console.log('run block of yourApp');
});
var myApp = angular.module('myApp',['yourApp']);
myApp.run(function(){
console.log('run block of myApp');
});
var someonesApp = angular.module('someonesApp',[]);
someonesApp.run(function(){
console.log('run block of someonesApp');
});
</script>
</body>
</html>