angularjs - ui-bootstrap 模态范围错误

标签 angularjs angular-ui-bootstrap

我注意到 ui-bootstrap 模态范围有一些奇怪的地方。似乎在其中使用 ng-model 时,您必须引用 $parent 才能到达模态 Controller 的范围。请注意,在我的 plunker 中,其他属性(例如 ng-options)不需要 $parent: http://plnkr.co/edit/xGSHz4EkZvGr2D6CUeBz?p=preview

知道为什么吗?我在这里发现了类似的问题: Scope issues with Angular UI modal

这导致我尝试 $parent 更改,但我无法对该线程发表评论,因为我没有足够的声誉。

知道范围为何发生变化吗?

谢谢!

最佳答案

模式有自己的范围(我从未使用过 Angular UI,但这是唯一可能发生的事情),当您设置“selectedLocation”时,该属性将在模式的范围上设置,而不是在 Controller 的范围上设置范围。 $parent 强制它获取 Controller 的范围,但这不是一个好的解决方案,因为您将自己锁定在某个结构中,始终假设模态的父级具有“模型”。

这是一个修改后的 Plunker,使用 Controller 范围上的模型对象(使用 model.selectedLocation) http://plnkr.co/edit/B5kZaIA5xi2RediUTBK7?p=preview

无论如何,如果您将属性放在“$scope.model.selectedLocation”之类的位置上,就会改变行为。现在,当我在模态上引用“model.selectedLocation”时,模态的作用域没有模型对象,因此 Angular 会沿着作用域链向上到达 Controller 的作用域(它有一个模型对象)。

观看约翰·林德奎斯特 (John Lindquist) 的这段视频,我认为它比我能更好地解释这一点。 :-) http://egghead.io/lessons/angularjs-the-dot

关于angularjs - ui-bootstrap 模态范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18991998/

相关文章:

javascript 不能正确地将 angular ui datepicker 日期转换为 UTC

angularjs - Protractor 元素浏览器

javascript - Angular 重新加载嵌入式 Controller

angularjs - 通过 ng-click 传递的 $event 未定义

angularjs - 使用angularjs ui bootstrap分页重新加载后如何留在当前页面上?

mysql - 数据库状态的实时 View

javascript - Angular Js ui.bootstrap 选项卡自定义

css - Bootstrap 列 CSS

javascript - Angular ui bootstrap 工具提示隐藏

javascript - 带 Ui-bootstrap 的周选择器