繁体   English   中英

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

[英]Need to Default select an Angular JS Radio Button

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

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"

如果你想从controller.js控制然后使用

ng-checked="default_option"

并在controller.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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM