一尘不染

如何在AngularJS中设置单选按钮的默认值?

angularjs

我需要建立一个系统,以按选择的票数更改总价。我创建了一些单选按钮来选择票证编号。问题是未设置默认值,并且加载时结果为null。

<input type="radio" ng-model="people" value="1" checked="checked"><label>1</label>
<input type="radio" ng-model="people" value="2"><label>2</label>
<input type="radio" ng-model="people" value="3"><label>3</label>
<ul>
  <li>{{10*people}}€</li>
  <li>{{8*people}}€</li>
  <li>{{30*people}}€</li>
</ul>

请参阅我的jsfiddle。


阅读 254

收藏
2020-07-04

共1个答案

一尘不染

设置默认值peoplengInit

<div ng-app>
    <div ng-init="people=1" />
        <input type="radio" ng-model="people" value="1"><label>1</label>
        <input type="radio" ng-model="people" value="2"><label>2</label>
        <input type="radio" ng-model="people" value="3"><label>3</label>
    <ul>
        <li>{{10*people}}€</li>
        <li>{{8*people}}€</li>
        <li>{{30*people}}€</li>
    </ul>
</div>

演示:小提琴

2020-07-04