angularjs - NG-开关不与NG-模型 [英] angularjs - ng-switch does not bind ng-model

查看:117
本文介绍了angularjs - NG-开关不与NG-模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个摄制 http://embed.plnkr.co/nVCmukG5abpi1Y4ZHkrq 这个节目当我点击'TITLE3,并输入在文本框中输入值,虽然输入的值显示体现在UI,当我点击点击按钮,没有什么是绑定的scope属性$ scope.test。

I have this repro http://embed.plnkr.co/nVCmukG5abpi1Y4ZHkrq that show when I click 'Title3' and enter a value in text box although the entered value shows reflected in the UI, when I click the 'click' button nothing is binded for the scope attribute $scope.test.

我不知道什么是错的NG-开关或者也许我做错了什么。帮助是AP preciated!

I don't know what is wrong with ng-switch or maybe I'm doing something wrong. Help is appreciated!!!

http://embed.plnkr.co/nVCmukG5abpi1Y4ZHkrq

推荐答案

这是一个范围的继承问题,由于 NG-开关创建它自己的范围。

This is a scope inheritance problem due to ng-switch creating it's own scope.

有是弹出很多是始终使用的车型之一建议。其原因是,当控制器范围内产品的对象,而不是原始的,子范围将创建到intiial对象的引用。如果模型是一种原始它不会更新原始。

There is one recommendation that pops up a lot is to always use a dot on models. The reason is that when the controller scope item is an object and not a primitive, sub scopes will create a reference to the intiial object. If model is a primitive it will not update the original.

例如:

<input ng-model="test.value" placeholder="pre" type="text" />

$scope.test={value:''}

另一种方法是在HTML模型标记使用 $父

<input ng-model="$parent.test" placeholder="pre" type="text" />

使用方法是一个很好的做法,以避免这些问题,因为你并不需要去思考更深层次的嵌套的作用域。

Using the dot methodology is a good practice to avoid these issues as you don't need to think about deeper nested scopes.

演示 test.value 为模型:<一href=\"http://plnkr.co/edit/CkiF55bLXsYzR6ZjcrJp?p=$p$pview\">http://plnkr.co/edit/CkiF55bLXsYzR6ZjcrJp?p=$p$pview

对于参考斑点在模型(有价值的阅读):<一href=\"https://github.com/angular/angular.js/wiki/Understanding-Scopes\">https://github.com/angular/angular.js/wiki/Understanding-Scopes

Reference regarding dot in models(valuable reading): https://github.com/angular/angular.js/wiki/Understanding-Scopes

这篇关于angularjs - NG-开关不与NG-模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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