需要默认选择一个角JS单选按钮 [英] Need to Default select an Angular JS Radio Button
问题描述
我是新来的角JS和我试图创建一组单选按钮。创建按钮是比较容易的部分,但我有搞清楚如何默认选择其中之一没有打破一切问题。我看了一下在角文档和多个其他计算器使用的问题,但ngChecked还是不太似乎看着办吧。
我需要做的是描述preselected之一,当用户来到该页面。价格,折扣,deal_value还需要从preselected price_option对象中的值显示。
下面是我试图去工作code: http://jsfiddle.net / qWzTb / 311 /
HTML code:
<机身NG-应用=demoApp>
< DIV NG控制器=DemoControllerNG-的init =的init([{数量:1,价格:10,qty_description:记述1,打折:1%,deal_value:200},{数量:2 ,价格:7,qty_description:记述2',折扣:'5%',deal_value:100}])> < DIV>
<标签NG重复=price_option在price_options>
<输入类型=电台NG-模式=init.price_optionNG值=price_optionNG-检查=selected_price_optionNAME =量/>
{{price_option.qty_description}}
< /标签>
< UL>
<立GT; {{init.price_option.price}}< /李>
<立GT; {{init.price_option.discount}}< /李>
<立GT; {{init.price_option.deal_value}}< /李>
< / UL>
< / DIV>
< / DIV>
< /身体GT;
使用Javascript:
angular.module('demoApp',[])。控制器('DemoController',函数($范围){ $ scope.init =功能(价格){
$ scope.price_options =价格;
$ scope.selected_price_option = $ scope.price_options [0];
};
});
HTML
<机身NG-应用=demoApp>
<! - NG-初始化功能所属控制器 - >
< DIV NG控制器=DemoController>
<! - 移动NG-重复到容器div,而不是标签 - >
< DIV NG重复=price_option在price_options>
<标签>
<! - 该模型的范围变量保存选定的价值 - >
<! - 这个数值就是这个特殊选项的值 - >
&LT;输入类型=电台NG-模式=$ parent.selected_price_optionNG值=price_optionNAME =量/>{{price_option.qty_description}}</label>
&LT; UL&GT;
&LT;李NG绑定=price_option.price&GT;&LT; /李&GT;
&LT;李NG绑定=price_option.discount&GT;&LT; /李&GT;
&LT;李NG绑定=price_option.deal_value&GT;&LT; /李&GT;
&LT; / UL&GT;
&LT; / DIV&GT;
&LT; / DIV&GT;
&LT; /身体GT;
JS
angular.module('demoApp',[])。
控制器('DemoController',函数($范围){
//初始化移动逻辑CONTROLER
$ scope.price_options = [{
数量:1,
价格:10,
qty_description:记述1',
折扣:1%,
deal_value:200
},{
数量:2,
价格:7,
qty_description:记述2',
折扣:'5%',
deal_value:100
}];
$ scope.selected_price_option = $ scope.price_options [1];
});
叉形小提琴: http://jsfiddle.net/W8KH7/2/
或者你也可以使用对象的策略,以避免引用$父: http://jsfiddle.net/W8KH7/3/
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];
};
});
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/
这篇关于需要默认选择一个角JS单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!