当值来自AngularJs中的API时,如何将值设置为单选按钮

时间:2016-06-29 05:13:23

标签: angularjs radio-button

我有3个收音机按钮

<input type="radio" name="rdoType" ng-model="result.visible" value="Gold">Gold
<input type="radio" name="rdoType" ng-model="result.visible" value="Silver">Silver
<input type="radio" name="rdoType" ng-model="result.visible" value="Copper">Copper

现在我通过添加ng-init

设置默认选择的Gold
<span class="textValue" ng-init="result.visible = 'Gold'>
    <input type="radio" name="rdoType" ng-model="result.visible" value="Gold">Gold
    <input type="radio" name="rdoType" ng-model="result.visible" value="Silver">Silver
    <input type="radio" name="rdoType" ng-model="result.visible" value="Copper">Copper
</span>

这会将Gold设置为默认选择的单选按钮。但 现在,在编辑此页面时,我从API获取Silver,现在如何将Silver设置为选定的单选按钮。

1 个答案:

答案 0 :(得分:0)

在控制器内创建一个result.visible变量,以便我们可以为其分配更新的值。

SampleController.js

function ($scope) {
     var $scope.result =  {
           'visible':Gold;
     }
     var $scope.updateVisible = function () {
          result['visible'] = Silver
     }
}

SampleHtml.html

<span class="textValue">
    <input type="radio" name="rdoType" ng-model="$ctrl.result.visible" value="Gold">Gold
    <input type="radio" name="rdoType" ng-model="$ctrl.result.visible" value="Silver">Silver
    <input type="radio" name="rdoType" ng-model="$ctrl.result.visible" value="Copper">Copper
</span>

看看是否有帮助