一页上的angularjs多个控制器 [英] angularjs multiple controllers on one page

查看:27
本文介绍了一页上的angularjs多个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个控制器的页面,其中一个控制器在同一页面内的 2 个不同 div 中使用.我不确定这是否是范围问题,或者我只是遗漏了代码中的某些内容.

I have a page with multiple controllers, one of the controller is being used in 2 different divs within the same page. I am not sure if it is a scope issue or I just miss something in my code.

这里是plunkrhttp://plnkr.co/edit/IowesXE3ag6xOYfB6KrN?p=preview

我想在用户点击储蓄"链接时隐藏文本框,点击成本"链接时显示该框.

I want to hide the textbox when the user clicks on 'Savings' link, display the box when clicking on 'Cost' link.

推荐答案

每次使用 ng-controller 时,都会为该控制器创建一个新实例,并拥有自己的作用域.如果您在 body 标签(或新的父标签)上设置 subCCtrl,并将其从它当前所在的两个 div 中删除,它对我有用.

Every time you use ng-controller, you make a new instance of said controller, with it's own scope. If you set subCCtrl on the body tag (or a new parent), and remove it from the two divs it is currently on, it works for me.

您可能想要查看的其他解决方案是将hideThisBox"保留在根范围内,在单击保存时广播事件或将其保留在服务中.

Other solutions you might want to look in to are by keeping "hideThisBox" on the root scope, broadcasting an event when clicking on save or by keeping it in a service.

这篇关于一页上的angularjs多个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆