1

I would like ChildCtrl to be initialized only when the button above it is clicked. how is that possible?

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

1 Answer 1

3

That is easy, use 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 directive does not load the DOM unless the expression is true.

Sign up to request clarification or add additional context in comments.

1 Comment

@YossiShasho keep in mind that ng-if creates an isolated scope. If ParentCtrl added thing to it's scope, then that is now accessed as $parent.thing inside ChildCtrl.scope because of ng-if.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.