我有2个项目列表,新闻和人物。
这两个列表具有不同的逻辑,位于不同的页面中,所以不要分享羞耻ng-app
:
<!-- News.html -->
<div id="newsList" ng-app="newsListApp">
<div id="{{news.id}}" ng-app="modalApp" ng-repeat="news in newsList">
...
</div>
</div>
<!-- People.html -->
<div id="peopleList" ng-app="peopleListApp">
<div id="{{person.id}}" ng-app="modalApp" ng-repeat="person in people">
...
</div>
</div>
我需要在模态弹出框中打开每个项目,因此我必须使用modalApp
,但我想使用相同的modalApp
(将使用< news
和people
列表中的em>相同的弹出式模板...
在这种情况下共享角度应用程序/模块的概念是什么?
答案 0 :(得分:2)
您无法使用ng-app
执行此操作。您必须手动引导其中至少一个。有关详细信息,请参阅this question。
答案 1 :(得分:2)
我认为你混淆了ng-app
和ng-controller
。可能你正在寻找这样的东西。通常,angular只需要每页一个应用程序,它们当然不应该嵌套
<body ng-app="myApp">
<div id="newsList" ng-controller="newsListCtrl">
<div id="{{news.id}}" ng-controller="modalCtrl" ng-repeat="news in newsList">
...
</div>
</div>
<div id="peopleList" ng-controller="peopleListCtrl">
<div id="{{person.id}}" ng-controller="modalCtrl" ng-repeat="person in people">
...
</div>
</div>
</body>
https://docs.angularjs.org/api/ng/directive/ngController
修改强>
如果news
和people
是不同的页面,那么您需要某种路由器。您的主要选项是ngRoute或ui-router。我认为ui-router比我更灵活。一些可能有用的有用链接:
https://scotch.io/tutorials/angular-routing-using-ui-router
http://plnkr.co/edit/dd8Nk9PDFotCQu4yrnDg?p=preview