Update parent scope variable in AngularJS

后端 未结 5 1594
不思量自难忘°
不思量自难忘° 2020-11-27 10:45

I have two controllers, one wrapped within another. Now I know the child scope inherits properties from the parent scope but is there a way to update the parent scope variab

5条回答
  •  孤街浪徒
    2020-11-27 11:29

    When you assign a primitive attribute to a scope, it is always local to the scope (possibly created on the fly), even if a parent scope has an attribute with the same name. This is a design decision, and a good one IMHO.

    If you need to change some primitive (ints, booleans, strings) in the parent scope, from the view, you need it to be an attribute of another object in that scope, so the assignment may read:

    Click me!
    

    and it will, in turn:

    1. get the viewData object from whatever scope it is defined in
    2. assign 4 to its myAttr attribute.

提交回复
热议问题