Ionic - 默认选中2个单选按钮(不同列表)

时间:2015-06-26 11:37:53

标签: javascript angularjs controller radio-button ionic

我目前正在Ionic采取我的第一步,并试图创建一个应用程序。

在这个example中,我有2个单选按钮列表(带有选项1,2,3和选项4,5,6):

enter image description here

我希望默认情况下检查 单选按钮2和4。

为什么只选中单选按钮4?

我错过了什么?

以下代码。

从模型HTML:

<ion-view view-title="Radio button demo">
  <ion-header-bar align-title="left" class="bar-positive">
    <h1 class="title">Chosen option: {{item}}, {{item2}}</h1>
   </ion-header-bar>  
 <ion-content>
<ion-list>
  <ion-radio ng-repeat="i in items" ng-value="i.Id" ng-model="item.itemval" >{{i.Name}}</ion-radio>
</ion-list>
<ion-list>
  <ion-radio ng-repeat="i in items2" ng-value="i.Id" ng-model="item2.itemval" >{{i.Name}}</ion-radio>
</ion-list>
  </ion-content>
</ion-view>

来自控制器javascript文件:

.controller("MainCtrl", function($scope) {
  $scope.items = [
    { Id: 1, Name: "Test 1", value: "t1" },
    { Id: 2, Name: "Test 2", value: "t2" },
    { Id: 3, Name: "Test 3", value: "t3" }
  ];

  $scope.items2 = [
    { Id: 4, Name: "Test 4", value: "t4" },
    { Id: 5, Name: "Test 5", value: "t5" },
    { Id: 6, Name: "Test 6", value: "t6" }
  ];

  $scope.item = { itemval: "2" };
  $scope.item2 = { itemval: "4" };
});

1 个答案:

答案 0 :(得分:2)

您需要在name上设置ion-radio属性,以表示它们属于不同的群组。

app.html中进行这些更改以使其有效:

<ion-view view-title="Radio button demo">
  <ion-header-bar align-title="left" class="bar-positive">
    <h1 class="title">Chosen option: {{item}}, {{item2}}</h1>
  </ion-header-bar>  
  <ion-content>
    <ion-list>
      <ion-radio ng-repeat="i in items" ng-value="i.Id" ng-model="item.itemval" name="group1">{{i.Name}}</ion-radio>
    </ion-list>
    <ion-list>
      <ion-radio ng-repeat="i in items2" ng-value="i.Id" ng-model="item2.itemval" name="group2">{{i.Name}}</ion-radio>
    </ion-list>
  </ion-content>
</ion-view>

在这里编辑了plunker:http://plnkr.co/edit/8IU4xskZOiOWcNScprHh?p=preview