需要默认选择Angular JS单选按钮

时间:2014-04-03 19:06:00

标签: javascript html angularjs

我是Angular JS的新手,我正在尝试创建一组单选按钮。创建按钮是很容易的部分,但我在确定如何默认选择其中一个而不破坏所有内容时遇到问题。我已经阅读过关于在Angular文档中使用ngChecked以及其他多个stackoverflow问题,但仍然无法理解它。

我需要做的是在用户访问页面时预先选择其中一个描述。 price,discount和deal_value也需要与预选的price_option对象中的值一起显示。

以下是我要尝试的代码:http://jsfiddle.net/qWzTb/311/

HTML代码:

<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];
  };
});

3 个答案:

答案 0 :(得分:8)

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/

答案 1 :(得分:3)

一般建议,避免使用'{{}}'使用ng-bind(这与问题无关,这只是一个好习惯)

使用

ng-value="true"

如果你想从controller.js控制,那么使用

ng-checked="default_option"

并在controller.js中控制它

$scope.default_option = true

答案 2 :(得分:0)

尝试这种方法:

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/