0

i wona get price and name of chosen radio buttons. its easy with simple html tags. But I stacked when i trying generate radio buttons via angularjs from array (items)

Help!

http://jsfiddle.net/hanze/j9x23apu/

html

<h1>Select </h1>

<div ng-app="" ng-controller="OrderCtrl">
      <div ng-repeat="item in items">
<div class="radio">
<label>
     <input type="radio" name="item" ng-model="item" ng-checked="{{item.checked}}"> 
     {{item.name}} +{{item.price}} $.</label>
</div>
</div>

Your choice: {{}} **what i must write here?**
<br>
Price: {{}} **and here?**
</div>

js

 OrderCtrl = function ($scope) {
$scope.items = [{
name: 'None',
value: "no",
price: 0,
checked: true
}, {
name: 'Black',
value: "black",
price: 99,
checked: false
}, {
name: 'White',
value: "white",
price: 99,
checked: false
}, {
name: 'Barhat',
value: "barhat",
price: 49,
checked: false
}, {
name: 'Barhat',
value: "cream",
price: 49,
checked: false
}]

3 Answers 3

1

You can look at the angularjs documentaion about radio buttons here. You don't need to use ng-checked here. Use ng-value to set the value when redio is selected. I changed your jsfiddle post.

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

8 Comments

your JSFiddle does not work. I have updated yours to work as well. jsfiddle.net/alansouzati/j9x23apu/12
Sorry i did not save my fiddle. please check again
using $parent is not safe. use an object as your model.
why $parent is not safe?
this is kind of duplicate of stackoverflow.com/questions/14775981/…
|
0

Your are missing the closing tag for your input. And when you have ng-repeat you have a seperate scope. In this case you need $parent.selectedItem to hold your selected elements.

I have updated the JSFiddle with a working state solution.

<div ng-app="" ng-controller="OrderCtrl">
<div ng-repeat="item in items" style="text-indent: 30px">
     <input type="radio" name="itemRadio"  ng-model="$parent.selectedItem" ng-value="item.name"/>
    {{item.name + '-' + item.price}}$.
</div>
    Your choice: {{selectedItem}}
</div>

Comments

0

jsFiddle: http://jsfiddle.net/j9x23apu/16/

html

<div ng-app="" ng-controller="OrderCtrl">
    <div ng-repeat="item in items" style="text-indent: 30px">
        <label>
            <input type="radio" name="item" ng-checked="{{item.checked}}" ng-click="change_item($event)" item-name="{{item.name}}" item-price="{{item.price}}" /> {{item.name}} + {{item.price}} 
        </label>
    </div>

    Your choice: {{selectedName}}
    <br />
    Price: {{selectedPrice}}
</div>

js

OrderCtrl = function ($scope) {
$scope.change_item = function(e) {
    $scope.selectedName = e.target.attributes['item-name'].value;
    $scope.selectedPrice = e.target.attributes['item-price'].value;
};

$scope.items = [{
    name: 'None',
    value: "no",
    price: 0,
    checked: true
}, {
    name: 'Black',
    value: "black",
    price: 99,
    checked: false
}, {
    name: 'White',
    value: "white",
    price: 99,
    checked: false
}, {
    name: 'Barhat',
    value: "barhat",
    price: 49,
    checked: false
}, {
    name: 'Barhat',
    value: "cream",
    price: 49,
    checked: false
}]
}

2 Comments

Why you don't put ng-value="{{item}}" and ng-model="$parent.something" inside your radio button input element?
that's not better, that is the only way to do that, you are forcefully using event instead of doing it in simpler way

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.