5

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 one of them without breaking everything. I have read about using ngChecked in the Angular docs and on multiple other stackoverflow questions but still can't quite seem to figure it out.

What I need to do is have one of the descriptions preselected when the user comes to the page. The price, discount, and deal_value also need to be shown with the values from the preselected price_option object.

Here is the code that I am trying to get to work: http://jsfiddle.net/qWzTb/311/

HTML Code:

<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 Answers 3

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

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/

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the quick response! I am trying to get this to work using the initializer function but can't figure out the scoping with init(). I need the initializer bc I am passing information via ruby from the view to set the price_options.
You can just run code then in ng-init like you would in the controller excpet you omit $scope. ng-init="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}];selected_price_option = price_options[1]".
This revision shows the same functionality as revision 3 except uses ng-init: jsfiddle.net/W8KH7/4
Whew, finally got it working! Thanks for all the help!
3

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

1 Comment

Thanks for the tip! I updated my code to use ng-bind instead of '{{}}'
0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.