1

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>
4

1 回答 1

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指令不会加载 DOM,除非表达式是true.

于 2014-12-25T13:01:26.673 回答