【问题标题】:Angular Material: Setting default option from radio button groupAngular Material:从单选按钮组设置默认选项
【发布时间】:2016-10-30 08:13:26
【问题描述】:

我一直在玩 Angular Material。我认为它很棒,但我不知道如何为单选按钮列表设置默认值。我尝试设置值和文本以及 Selected 但没有运气。

$scope.rdButton.value = 'Yes';
$scope.rdButton.text = 'Yes';

rdButton 是 ng-model 组的名称。关于如何做到这一点的任何想法?

谢谢

【问题讨论】:

    标签: angularjs radio-button angular-material


    【解决方案1】:

    如果rdButton 是ng-model,只需设置$scope.rdButton = 'Yes'; 以将具有值Yes 的无线电设置为默认值。

    【讨论】:

    • 当这些超级简单的东西占了我的便宜时,我总是很烦。这就像一个魅力。感谢 TJ 和 akazemis 的协助。我很感激。
    【解决方案2】:

    假设您在 HTML 中有这样的内容:

    <input type="radio" ng-model="rdButton" value="Yes">
    Yes
    </input>
    <input type="radio" ng-model="rdButton" value="No">
    No
    </input>
    

    您只需要在控制器中执行此操作:

    $scope.rdButton= 'Yes';
    

    【讨论】:

    • 效果很好。谢谢。
    【解决方案3】:

    只要提到你想要选择默认元素的 ng-model

    JS

     $scope.price_options = [{
            price: 10
        }, {
            price: 7
        }];
        $scope.selected_price_option = $scope.price_options[1];
    

    HTML

    <div ng-repeat="price_option in price_options">
                <label>
                    <input type="radio" ng-model="$parent.selected_price_option" ng-value="price_option" name="quantity" />{{price_option.price}}</label>
            </div>
            {{selected_price_option}}
        </div>
    

    供参考https://plnkr.co/edit/8XRnXb49cbXZpHhxBLW2?p=preview

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-02
      • 2017-04-05
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      • 2018-04-21
      • 1970-01-01
      • 2018-06-26
      相关资源
      最近更新 更多