我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>
我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>
这很简单,使用 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
指令不会加载 DOM,除非表达式是true
.