【问题标题】:How to set value to radio button when value is coming from API in AngularJs当值来自AngularJs中的API时如何将值设置为单选按钮
【发布时间】:2016-06-29 13:27:28
【问题描述】:

我有 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 设置为选中的单选按钮。

【问题讨论】:

  • 你可以为它制作jsfiddle吗?

标签: angularjs radio-button


【解决方案1】:

在控制器中创建一个 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>

看看有没有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-27
    • 2017-06-04
    • 2015-04-21
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    相关资源
    最近更新 更多