Need to Default select an Angular JS Radio Button

后端 未结 3 1845
刺人心
刺人心 2021-01-19 01:00

I am new to Angular JS and I am trying to create a set of radio buttons. Creating the buttons was the easy part, but I am having problems figuring out how to default select

相关标签:
3条回答
  • 2021-01-19 01:35

    General suggestion, avoid using '{{}}' use ng-bind (This will not related to question, its just good practice )

    Use

    ng-value="true"
    

    If you want to control from controller.js then use

    ng-checked="default_option"
    

    and control it in controller.js

    $scope.default_option = true
    
    0 讨论(0)
  • 2021-01-19 01:39

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

    Forked fiddle: http://jsfiddle.net/W8KH7/2/

    Or you can use the object strategy to avoid having to reference $parent: http://jsfiddle.net/W8KH7/3/

    0 讨论(0)
  • 2021-01-19 01:58

    Try this approach:

    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>
    

    Demo: http://jsfiddle.net/qWzTb/313/

    0 讨论(0)
提交回复
热议问题