简体   繁体   English

AngularJS意外共享范围

[英]AngularJS Accidental Sharing of Scopes

I'm having a problem in AngularJS where my parent controller and child controller are sharing the same model. 我在AngularJS中遇到问题,我的父控制器和子控制器共享相同的模型。 In this examples, there is recursive comments: 在这个例子中,有递归注释:

<div ng-controller="ParentController">
     <label>Comment</label>
     <textarea name="comment_text" ng-model="comment_text"></textarea>
     <input type="submit" value="Leave Comment" ng-click="sendComment($event)" />
     <div class="replies">
        <div class="areply" ng-controller="ChildController">
          Someone said: blah blah blah
          <label>Reply</label>
          <textarea name="comment_text" ng-model="comment_text"></textarea>
          <input type="submit" value="Leave Comment" ng-click="sendComment($event)" />
        </div>
     </div>

</div>

1st question....am I doing it wrong? 第一个问题....我做错了吗? And my second is there a way to make sure the parent and child scopes with ng-model do not affect each other? 我的第二种方法是确保使用ng-model的父和子范围不会相互影响?

Child controller scopes are prototypical descendants of their parent controllers' scopes. 子控制器作用域是其父控制器作用域的原型后代。 It's an intentional feature of Angular. 这是Angular的一个故意特征。 The child scope has access to the parent's state, but not the other way around. 子范围可以访问父级的状态,但不能相反。

If you don't want this behaviour there are a couple of options - either don't use nested controllers (you can use custom services to share data where you need to, which is more test friendly and therefore considered best practice), or, more simply, just call the parent and child scope fields different names. 如果你不想要这种行为,有几个选项 - 要么不使用嵌套控制器(你可以使用自定义服务在你需要的地方共享数据,这样更适合测试,因此被认为是最佳实践),或者,更简单地说,只需调用父和子范围字段的不同名称。

The code that you show is clearly too abstract to give you a more concrete answer, but it's important to notice that a child controller extends the behaviour of its parent (as a child class with its parent in OOP). 您显示的代码显然过于抽象,无法为您提供更具体的答案,但重要的是要注意子控制器扩展其父级的行为(作为子级,其父级在OOP中)。

Therefore, a same variable should play the same role in a child controller and in its parent. 因此,同一个变量应该在子控制器及其父控件中扮演相同的角色。 If you want a different behaviour, you should use another name. 如果您想要不同的行为,则应使用其他名称。

Why not just make your child text area use a different model, and have sendComment take an argument for which comment to accept? 为什么不让你的孩子文本区域使用不同的模型,并让sendComment接受哪个评论接受的参数?

<textarea name="comment_text" ng-model="comment_text"></textarea>
<input type="submit" value="Leave Comment" ng-click="sendComment($event, comment_text)" />
...
    <textarea name="comment_text" ng-model="child_comment_text"></textarea>
    <input type="submit" value="Leave Comment" ng-click="sendComment($event, child_comment_text)" />

Otherwise, you could create a directive that uses an isolate scope. 否则,您可以创建使用隔离范围的指令。 Try a tutorial that goes over isolate scopes and directives, or whatch a screencast, such as those from egghead.io . 尝试一个覆盖隔离范围和指令的教程,或者一个截屏视频,例如来自egghead.io的那些。

I would take joews second answer and not use nested Controllers, you don't need to. 我会接受joews的第二个回答,而不是使用嵌套的控制器,你不需要。 It's best practice to keep these separate. 保持这些分开是最好的做法。 Generally each "page" would have it's own controller that dictates that pages functionality. 通常,每个“页面”都有自己的控制器来指示页面功能。 You can use services and other angularjs resources to make sure you aren't rewriting code all the time. 您可以使用服务和其他angularjs资源来确保您不会一直重写代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM