使用 AngularJS 将所选单选按钮以外的值设为假

False the value except the selected radio button using AngularJS

我在我的 angular 页面中使用 radio 按钮。单选按钮位于 ng-repeat 下方。我想通过 json 数据检查收音机默认值是 true。然后,如果检查了一个无线电,则将剩余数据更改为 false。我做到了。但是 UI.

中没有选中单选按钮

Here a fiddle link

我希望你明白这一点以及我需要什么。

将您的 $scope.setChoiceForQuestion 更改为:

$scope.setChoiceForQuestion = function (q, c) {        
   angular.forEach(q, function (r) {
        if(c != r)
        r.isUserAnswer = false;
    });
};

它应该可以工作。

使用 ng-checked="choise.isUserAnswer || choise.isUserAnswer === 'true'" 来检查和取消检查元素而不是 ng-model="choice.isUserAnswer"'true' 比较,因为在 json 中你有字符串。或者您可以将 json "true" 和 "false" 更改为 truefalse 不带双引号并保留条件 ng-checked="choise.isUserAnswer".

Here a fiddle link