在以下示例中,有三个模块,第二个模块需要第一个模块。这在使用ng-app
in引用时有效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>