2013-03-06 22 views
1

为什么我不能从ng-include中的html中设置控制器ExampleCtrl的$ scope.myProperty的值。但是,如果我定义$ scope.myObj.myProperty和在HTML我推荐像ng-model="myObj.myProp"工作正常?AngularJS:ng-include和示波器

例子:

<div ng-app="MyApp"> 
    <div ng-controller='ExampleCtrl'> 
     <div ng-include="'#inlcude'"></div> 
     <!-- THIS DON'T WORK --> 
     <p><b>myProperty:</b>{{myProperty}}</p> 
     <!-- THIS WORK --> 
     <p><b>myObj.myProperty:</b>{{myObj.myProperty}}</p> 
    </div> 
</div>  
<!-- INCLUDE HTML --> 
<div id='include'> 
    <input ng-model='myProperty' type='text' /> 
    <input ng-model='myObj.myProperty' type='text' /> 
</div> 

我明白泰德NG-包括创建一个新的范围,但为什么从的HTML包括我没有看到你的父母范围的简单欢迎使用属性?

感谢

回答

1

当输入写入myProperty将(在这一点上)上创建子作用域属性。此子范围属性隐藏/隐藏相同名称的父属性。孩子做出的任何更改都将在子范围属性上进行。父范围不能看到这个新的子范围属性–它不知道这个其他属性存在。当插入{{myProperty}}时,它会找到它自己的属性。

当其他输入写入myObj.myProperty时,它会跟随原型链并写入父范围的属性。

对于一个更详细的答案(有很多图片),请参阅What are the nuances of scope prototypal/prototypical inheritance in AngularJS?

+1

谢谢!我发现你的[post](http://stackoverflow.com/questions/14049480/what-are-the-nuances-of-scope-prototypal-prototypical-inheritance-in-angularjs) – jlarghi 2013-03-06 20:22:59