59

我需要建立一个系统来根据所选票数更改总价。我创建了一些单选按钮来选择票的数量。问题是默认值未设置,加载时结果为空。

<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}}€&lt;/li>
  <li>{{8*people}}€&lt;/li>
  <li>{{30*people}}€&lt;/li>
</ul>

请看我的jsfiddle。

4

5 回答 5

117

people使用ngInit设置默认值

<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}}€&lt;/li>
        <li>{{8*people}}€&lt;/li>
        <li>{{30*people}}€&lt;/li>
    </ul>
</div>

演示:小提琴

于 2013-04-05T12:21:13.203 回答
24
<div ng-app="" ng-controller="myCntrl">    
        <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>
</div>
<script>
    function myCntrl($scope){
        $scope.people=1;
    }
</script>
于 2014-10-14T06:40:30.747 回答
14

为什么不只是ng-checked="true"

于 2016-03-27T02:07:05.213 回答
1

在 Angular 2 中,我们可以这样设置单选按钮的默认值:

HTML:

<label class="form-check-label">
          <input type="radio" class="form-check-input" name="gender" 
          [(ngModel)]="gender" id="optionsRadios1" value="male">
          Male
</label>

在组件类中,将“性别”变量的值设置为单选按钮的值:

gender = 'male';
于 2018-04-08T11:55:52.220 回答
-6
<input type="radio" name="gender" value="male"<%=rs.getString(6).equals("male") ? "checked='checked'": "" %>: "checked='checked'" %> >Male
               <%=rs.getString(6).equals("male") ? "checked='checked'": "" %>
于 2016-10-04T14:57:22.900 回答