【发布时间】:2015-02-24 00:14:02
【问题描述】:
我的 Angular 应用程序中有几个模板,我打算动态插入它们,其中一些可能需要共享数据,甚至可能使用同一个控制器。
我最近看到了很多“ng-controller as”语法的例子来实现这一点,我读过的大部分内容都支持这种方法,但我不知道,我对此感到怀疑它。
我想知道做这样的事情有什么优点和/或缺点(每个 div 是一个插入的 sn-p/模板):
<div ng-controller="mainController as main">
mainController: <input type="text" ng-model="main.someValue" />
main.someValue: {{ main.someValue }}
<div ng-controller="anotherController as another">
anotherController: <input type="text" ng-model="another.someValue" />
main.someValue: {{ main.someValue }}
</div>
</div>
当我可以做这样的事情时:
<div ng-controller="mainController">
mainController: <input type="text" ng-model="someValue" />
someValue: {{ someValue }}
</div>
<div ng-controller="anotherController">
anotherController: <input type="text" ng-model="anotherValue" />
someValue: {{ someValue }}
</div>
使用服务来连接我的两个模板中的数据。
谢谢!
【问题讨论】:
标签: javascript angularjs frontend