需要默认选择一个角度JS单选按钮

Need to Default select an Angular JS Radio Button

本文关键字:一个 JS 单选按钮 默认 选择      更新时间:2023-09-26

我是Angular JS的新手,我正在尝试创建一组单选按钮。 创建按钮是容易的部分,但是我在弄清楚如何在不破坏所有内容的情况下默认选择其中一个按钮时遇到了问题。 我已经在 Angular 文档和其他多个堆栈溢出问题中阅读了有关使用 ngChecked 的信息,但似乎仍然无法弄清楚。

我需要做的是在用户进入页面时预先选择其中一个描述。价格、折扣和deal_value还需要与预选price_option对象的值一起显示。

这是我尝试工作的代码:http://jsfiddle.net/qWzTb/311/

网页代码:

<body ng-app="demoApp">
    <div ng-controller="DemoController" ng-init="init([{qty: 1, price:10, qty_description:'descrip 1', discount:'1%', deal_value:200}, {qty: 2, price:7, qty_description:'descrip 2', discount:'5%', deal_value:100}])">  
       <div>
           <label ng-repeat="price_option in price_options">
              <input type="radio" ng-model="init.price_option" ng-value="price_option" ng-checked="selected_price_option" name="quantity"/>
              {{ price_option.qty_description }}
           </label>
           <ul>
              <li>{{ init.price_option.price }}</li>
              <li>{{ init.price_option.discount }}</li>
              <li>{{ init.price_option.deal_value }}</li>
           </ul>
        </div>
    </div>
</body>

Javascript:

angular.module('demoApp', []).controller('DemoController', function($scope) {
  $scope.init = function(prices) {
    $scope.price_options = prices;
    $scope.selected_price_option = $scope.price_options[0];
  };
});

HTML

<body ng-app="demoApp">
    <!-- ng-init functionality belongs in controller -->
    <div ng-controller="DemoController">
        <!-- move ng-repeat to container div instead of label -->
        <div ng-repeat="price_option in price_options">
            <label>
                <!-- the model is the scope variable holding the selected value -->
                <!-- the value is the value of this particular option -->
                <input type="radio" ng-model="$parent.selected_price_option" ng-value="price_option" name="quantity" />{{price_option.qty_description}}</label>
            <ul>
                <li ng-bind="price_option.price"></li>
                <li ng-bind="price_option.discount"></li>
                <li ng-bind="price_option.deal_value"></li>
            </ul>
        </div>
    </div>
</body>

.JS

angular.module('demoApp', []).
controller('DemoController', function ($scope) {
    //moved init logic to controler
    $scope.price_options = [{
        qty: 1,
        price: 10,
        qty_description: 'descrip 1',
        discount: '1%',
        deal_value: 200
    }, {
        qty: 2,
        price: 7,
        qty_description: 'descrip 2',
        discount: '5%',
        deal_value: 100
    }];
    $scope.selected_price_option = $scope.price_options[1];
});

分叉小提琴:http://jsfiddle.net/W8KH7/2/

或者,您可以使用对象策略来避免引用$parent:http://jsfiddle.net/W8KH7/3/

一般建议,避免使用"{{}}"使用ng-bind (这与问题无关,这只是很好的做法)

ng-value="true"

如果要从控制器进行控制.js请使用

ng-checked="default_option"

并在控制器中控制它.js

$scope.default_option = true

试试这种方法:

angular.module('demoApp', []).controller('DemoController', function ($scope) {
    $scope.priceOptions = [
        {qty: 1, price: 10, qty_description: 'Descrip 1', discount: '1%', deal_value: 200}, 
        {qty: 2, price: 7, qty_description: 'Descrip 2', discount: '5%', deal_value: 100}
    ];
    $scope.selected = {priceOption: $scope.priceOptions[0]};
});

.HTML

<label ng-repeat="priceOption in priceOptions">
    <input type="radio" ng-model="selected.priceOption" ng-value="priceOption" name="quantity" />
    {{ priceOption.qty_description }}
</label>
<ul>
    <li>{{ selected.priceOption.price }}</li>
    <li>{{ selected.priceOption.discount }}</li>
    <li>{{ selected.priceOption.deal_value }}</li>
</ul>

演示:http://jsfiddle.net/qWzTb/313/