search

Home  >  Q&A  >  body text

angular.js - angularjs 中的 controller 能否嵌套?

angularjs 里的 controller 能嵌套吗?

如:

<p ng-controller="ACtrl">
    <p ng-controller="BCtrl">
        <p>{{ value }}</p>
    </p>
</p>

在 BCtrl 中修改 $scope.value 的值,然后在 ACtrl 中使用貌似会报错 inprog。

漂亮男人漂亮男人2743 days ago953

reply all(5)I'll reply

  • PHP中文网

    PHP中文网2017-05-15 16:51:57

    can be nested, but you should use $parent variables.
    http://fdietz.github.io/recipes-with-angular-js/controllers/sharing-models-between-nested-controllers.html

    reply
    0
  • 阿神

    阿神2017-05-15 16:51:57

    http://stackoverflow.com/questions/21287794/angularjs-controller-as-syntax-clarification

    reply
    0
  • 黄舟

    黄舟2017-05-15 16:51:57

    It can be nested, and the syntax of "controller as ctrl" is better. If you think about it written like in the question, you don’t know whether the value comes from ACtrl or BCtrl

    <p ng-controller="ACtrl as a">
        <p ng-controller="BCtrl as b">
            <p>{{ b.value }}</p>
        </p>
    </p>
    

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-15 16:51:57

    It must be nestable, otherwise it will not easily shake the dominance of JQuery. If you really want to learn, I recommend the book Angular Authoritative Guide

    reply
    0
  • 阿神

    阿神2017-05-15 16:51:57

    The best book in China at present should be the Chinese version of ng-book. Read the book carefully. It’s much better than the era when you could only read documents through the wall.

    reply
    0
  • Cancelreply