简体   繁体   English

Angular radio ng-change仅在第一次工作

[英]Angular radio ng-change works only first time

I can't understand why angular ng-change function are called only in first click. 我不明白为什么只有在第一次单击时才调用角度ng-change函数。 In this example 在这个例子中

http://jsfiddle.net/ZPcSe/5/ http://jsfiddle.net/ZPcSe/5/

function is called every time when i change current radio selection, but in my code something is wrong 每次更改当前收音机选择时都会调用该函数,但是在我的代码中出现了问题

http://jsfiddle.net/4jL3u8ko/1/ http://jsfiddle.net/4jL3u8ko/1/

Can someone change my code to work like first example, and explain why it isn't working? 有人可以更改我的代码以使其像第一个示例一样工作,并解释为什么它不起作用吗?

Great question. 好问题。

This is not an answer but one may call this explanations to answers and any further problems that may occur. 这不是答案,但可以将此解释称为答案以及可能发生的任何其他问题。 And this happens quite often, so its important to understand. 而且这种情况经常发生,因此了解这一点很重要。 The answer given by @Victor is ok and the comment of @Atias is also helpful. @Victor给出的答案是可以的,@ Atias的评论也很有帮助。

Explanation - 说明-

Simple explanation : ng-repeat forms child scope. 简单说明:ng-repeat形成子范围。

Little detailed : 详细一点:

So your application has 3 scopes (or may increase depending on the number of values in $scope.radioButtons). 因此,您的应用程序具有3个作用域(或可能会增加,具体取决于$ scope.radioButtons中值的数量)。 Let me name them - parentScope(main scope), childScope1(scope of first element of ng-repeat) and childScope2(scope of second element of ng-repeat). 我给它们命名-parentScope(主作用域),childScope1(ng-repeat的第一个元素的作用域)和childScope2(ng-repeat的第二个元素的作用域)。

Parent scope variables : calledFunctions(array), radioButtons(object) and newValue(function) 父作用域变量:称为Functions(数组),radioButtons(对象)和newValue(function)

ChildScope1 variables : name(assigned a literal at the start), val(assigned an object at start) and value(it has no value, or undefined) ChildScope1变量:name(在开始时分配一个文字),val(在开始时分配一个对象)和value(它没有值或未定义)

ChildScope2 variables : name(assigned a literal at the start), val(assigned an object at start) and value(it has no value, or undefined) ChildScope2变量:name(在开始时分配一个文字),val(在开始时分配一个对象)和value(它没有值或未定义)

What happens when you click on the radio button of childScope1(for first time): 第一次单击childScope1的单选按钮时会发生什么:

ChildScope1.value = ChildScope1.name or ChildScope1.value= "Radio1"; ChildScope1.value = ChildScope1.name或ChildScope1.value =“ Radio1”; the ng-change directive checks if this scope's model has been changed? ng-change指令检查此作用域的模型是否已更改? Yes, as it was undefined and now it has a literal("Radio1")!! 是的,因为它是未定义的,现在有一个文字(“ Radio1”)! So call ChildScope1.newValue(), which obviously is not present. 因此,调用ChildScope1.newValue()显然不存在。 Therefore now look for parent- call parentScope.newValue(). 因此,现在寻找父调用parentScope.newValue()。 This is present, so execute it. 存在,所以执行它。

keep in mind that ChildScope1.value= "Radio1"; 请记住,ChildScope1.value =“ Radio1”;

After clicking on other radio buttons...... Now lets click 2nd time on ChildScope1's radio button. 单击其他单选按钮之后……现在让我们第二次单击ChildScope1的单选按钮。 After clicking it- ChildScope1.value = ChildScope1.name or ChildScope1.value= "Radio1"; 单击后-ChildScope1.value = ChildScope1.name或ChildScope1.value =“ Radio1”; the ng-change directive checks if this scope's model has been changed??? ng-change指令检查此作用域的模型是否已更改??? No!! 没有!! Because it still contains the same literal vaule as before, so do not even look for ChildScope1.newValue() or ParentScope.newValue() function. 因为它仍然包含与以前相同的字面值,所以甚至不要寻找ChildScope1.newValue()或ParentScope.newValue()函数。

Now same thing happens with ChildScope2. 现在,ChildScope2也发生了同样的事情。

Hope this explains why your fiddle works like a one time binding, or how @Victor or @Atias are correct. 希望这可以解释您的小提琴为何像一次绑定一样工作,或者@Victor或@Atias正确无误。

Simply the solution is - The ng-model of ChildScope1 or ChildScope2 or any other child scopes, should point to a variable of parent scope. 简单的解决方案是-ChildScope1或ChildScope2或任何其他子范围的ng-model应该指向父范围的变量。 So you can use $parent or make an object in parent scope(main scope) with a property( eg.- ParentVal={ChildVal = ""};). 因此,您可以使用$ parent或在具有属性的父作用域(主作用域)中创建对象(例如-ParentVal = {ChildVal =“”};)。 And in ng-model inside ng-repeat- write ng-model=ParentVal.ChildVal. 并在ng-repeat-内的ng-model中编写ng-model = ParentVal.ChildVal。

Sorry for my poor english and please pardon my spelling mistakes if there is any. 对不起,我的英语不好,请原谅我的拼写错误。

Thanks 谢谢

ng-model is tricky. ng-model非常棘手。 You should always, always have a dot in your models, or you get this kind of problem where you are creating several models and you think you only have one. 您应该始终在模型中始终有一个点,否则在创建多个模型而您认为只有一个时会遇到这种问题。

As a quick fix, use $parent.value instead of just value in your ng-model and ng-change . 作为快速解决方案,请使用$parent.value而不是ng-modelng-change value

As a good fix, have a proper model object in the scope, instead of storing values directly in the scope: 作为一个好的解决方案,在范围内有一个合适的模型对象,而不是直接在范围内存储值:

$scope.model = {};

ng-model="model.radiosValue"

As an even better fix, use the controllerAs pattern and use that object as the view model. 作为更好的解决方案,请使用controllerAs模式并将该对象用作视图模型。

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

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