我很迷惑.如果我ng-app
每页只能使用一次,我如何在不同的.js文件中使用位于不同模块中的指令.看:
<script src='mainModule.js'/>
<script src='secondModule.js'/>
<body ng-app='mainModule'>
<my-thingy/>
<div>
<other-thingy/>
</div>
</body>
mainModule.js:
angular.module("mainModule",[]).directive("myThingy",function(){ ... })
secondModule.js:
angular.module("secondModule",[]).directive("otherThingy",function(){ ... })
Run Code Online (Sandbox Code Playgroud)
那么,我现在如何指向页面上的secondModule,而不是从mainModule.js引用它
pko*_*rce 50
@Agzam,只需创建第三个顶级应用程序模块,它将依赖于包含指令的子模块:
<script src='mainModule.js'/>
<script src='secondModule.js'/>
<script>
angular.module('app', ['mainModule', 'secondModule'])
</script>
<body ng-app='app'>
<my-thingy/>
<div>
<other-thingy/>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
有一个解决方案可以以编程方式在页面上引导多个角度模块(文档)。
您必须ng-app
从 html 中删除属性。
定义几个模块:
var app1 = angular.module('myApp1', []);
var app2 = angular.module('myApp2', []);
app1.controller('MainCtrl', function($scope) {
$scope.name = 'App1';
});
app2.controller('MainCtrl', function ($scope) {
$scope.name = 'App2';
})
Run Code Online (Sandbox Code Playgroud)
然后在index.html中执行以下操作:
<html>
<head></head>
<body>
<!-- Module 1 -->
<div id="myApp1">
<div ng-controller="MainCtrl">
<h1>Hello {{name}}</h1>
</div>
</div>
<!-- Module 2 -->
<div id="myApp2">
<div ng-controller="MainCtrl">
<h1>Hello {{name}}</h1>
</div>
</div>
<!-- Bootstrap modules -->
<script>
angular.element(document).ready(function() {
angular.bootstrap(document.getElementById('myApp1'), ['myApp1']);
angular.bootstrap(document.getElementById('myApp2'), ['myApp2']);
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是一个使用此解决方案的工作plnkr。
归档时间: |
|
查看次数: |
9830 次 |
最近记录: |