角:单选按钮停止射击" NG-变化"每一个被点击后 [英] Angular: radiobuttons stop firing "ng-change" after each one was clicked

查看:157
本文介绍了角:单选按钮停止射击" NG-变化"每一个被点击后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是动态生成单选按钮。 NG-变化='为newValue(值)后,每个单选按钮已经pssed一旦$ P $停止被调用。

I am building radio buttons dynamically. ng-change='newValue(value) stops being called after each radio button has been pressed once.

这个工程:点击单选按钮变为富/酒吧/巴兹值。
http://jsfiddle.net/ZPcSe/19/

this works: Clicking on the radio buttons changes the value to foo/bar/baz. http://jsfiddle.net/ZPcSe/19/

<div ng-controller="MyCtrl">
<input type="radio" ng-model="value" value="foo" ng-change='newValue(value)'>
<input type="radio" ng-model="value" value="bar" ng-change='newValue(value)'>
<input type="radio" ng-model="value" value="baz" ng-change='newValue(value)'>    
<hr>
{{value}}
</div>

这code没有:本{{值}} - 标签是不是更新一次,每个单选按钮已经pssed至少一次$ P $。 Aparently NG-变化不会解雇了。

this code does not: The {{value}} - "label" is not updated once each radio button has been pressed at least once. Aparently ng-change is not fired any more.

<div ng-controller="MyCtrl">
    <span ng-repeat="i in [0, 1, 2]">
    <input name="asdf" type="radio" ng-model="value" value={{i}} ng-change='newValue(value)'>   
    </span>
    {{value}}
</div>

http://jsfiddle.net/ZPcSe/18/

该Controlles是一样的,每次:

The Controlles is the same each time:

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
    $scope.value = '-';
    $scope.newValue = function(value) {
    $scope.value = value;
    }
}

感谢您的帮助。

推荐答案

ngRepeat 创造新的范围,所以试图设置设置它的新范围。解决方法是引用一个对象,它是对父范围上的属性 - 对象查找发生在父范围,然后更改属性按预期工作:

ngRepeat creates new scope, so trying to set value sets it on the new scope. The workaround is to reference a property on an object that is on the parent scope--the object lookup happens on the parent scope, and then changing the property works as expected:

HTML

<div ng-controller="MyCtrl">
<span ng-repeat="i in [0, 1, 2]">
  <input name="asdf" ng-model="options.value" value="{{i}}" type="radio">
</span>
{{options.value}}

JS:

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
    $scope.options = {
        value: '-'
    };
    $scope.newValue = function(value) {
        // $scope.options.value = value; // not needed, unless you want to do more work on a change
    }
}​

您可以检查出这种解决方法的拨弄工作。请参见角/ angular.js#1100 GitHub上了解详情。

You can check out a working fiddle of this workaround. See angular/angular.js#1100 on GitHub for more information.

这篇关于角:单选按钮停止射击&QUOT; NG-变化&QUOT;每一个被点击后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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