我希望ChildCtrl
仅在单击上方的按钮时进行初始化。怎么可能?
<div ng-controller="ParentCtrl">
<button>CLICK TO INIT CHILD CTRL</button>
<div class="child-container">
<div ng-controller="ChildCtrl" ng-include="'templates/child.html'" />
</div>
</div>
答案 0 :(得分:3)
这很简单,请使用ng-if:
<div ng-controller="ParentCtrl">
<button ng-click='loadChild=true'>CLICK TO INIT CHILD CTRL</button>
<div class="child-container" ng-if='loadChild'>
<div ng-controller="ChildCtrl" ng-include="'templates/child.html'" />
</div>
</div>
除非表达式为ng-if
,否则 true
指令不会加载DOM。