【问题标题】:ng-model not updating with radio buttonng-model 不使用单选按钮更新
【发布时间】:2014-07-12 22:08:36
【问题描述】:

我遇到了角度问题,但我不明白问题可能是什么:

这是一个 div:

<div ng-controller="CountrySelectorController">
            Selected Countryid = {{countryid}}
            <div class="radio" ng-repeat="country in countries">
                <input type="radio" name="countryOptions" ng-model="countryid" value={{country.countryid}} ng-checked="countryid == country.countryid" /><span style="margin-left:10px;">{{country.countryid}}.{{country.name}}</span>
                </label>
            </div>
</div>

那是我的控制器:

app.controller('CountrySelectorController', function($scope, $rootScope){
    $scope.countryid = 1;
});

我遇到的问题: -Selected Countryid=1 出现在 start 。虽然我选择了不同的国家,但是模型没有更新

【问题讨论】:

  • 你不应该使用 ng-checked。
  • @dotnetstep,我用了checked,没问题,但是model不是udpdating,Selected Countryid总是1
  • @Braulio,我就是这样做的,使用具有相同型号、相同名称和不同值的单选按钮
  • 抱歉,Jerrad 是对的,避免 $parent 内容的另一种方法是将范围可见数据包装在“模型”中,例如 $scope.model.countryid,然后在您的 ng -model 你会使用类似 ng-model="model.countryid"

标签: angularjs


【解决方案1】:

ng-repeat 创建自己的范围,这不是您想要绑定 ng-model 的范围。您想将 ng-model 绑定到控制器的范围(这是 ng-repeat 的父范围)。 使用 $parent 将级别提升到正确的范围。另外,不要使用 ng-checked。

ng-model="$parent.countryid" 

Demo

【讨论】:

  • 谢谢,它有效,这个,ng-repeat 创建自己的范围的事实,我不知道
猜你喜欢
  • 2016-01-30
  • 2014-06-10
  • 2016-08-12
  • 2013-12-13
  • 1970-01-01
  • 2013-09-06
  • 2015-10-13
  • 1970-01-01
  • 2016-10-13
相关资源
最近更新 更多