Ionic + Angular默认情况下无法检查离子无线电

时间:2014-09-25 12:58:04

标签: javascript angularjs radio-button

我试图从收音机列表中的单选按钮检查一个,但没有运气。

有人可以告诉我我做错了吗?

感谢您的帮助。

我尝试这样做:

<div class="list">

      <ion-radio ng-repeat="item in goalTypeList"
                 ng-value="item.value"
                 ng-change="goalTypeChanged(item)"
                 ng-checked="item.selected"
                 ng-model="data.clientSide">
          {{ item.text }}
      </ion-radio>

  </div> 

JS:

.controller('SettingsCtrl', function($scope, $ionicLoading) {

        $scope.goalTypeList = [
            { text: "Dials", value: "dials", selected: true },
            { text: "Conversations", value: "conversations" , selected: false  },
            { text: "Appointments", value: "appointments" , selected: false },
            { text: "Orders", value: "orders", selected: false  }
        ];

        $scope.data = {
            clientSide: 'ng'
        };

        $scope.goalTypeChanged = function(item) {
            console.log("Selected goalType, text:", item.text, "value:", item.value);
        };

4 个答案:

答案 0 :(得分:2)

似乎data.client Side的值不在列表目标TypeList中。更改值以匹配任何..

HTML:

<div class="list">

  <ion-radio ng-repeat="item in goalTypeList"
             ng-value="item.value"
             ng-change="goalTypeChanged(item)"
             ng-checked="item.selected"
             ng-model="data.clientSide">
      {{ item.text }}
  </ion-radio>

JS:

  .controller('SettingsCtrl', function($scope, $ionicLoading) {

    $scope.goalTypeList = [
        { text: "Dials", value: "dials", selected: true },
        { text: "Conversations", value: "conversations" , selected: false  },
        { text: "Appointments", value: "appointments" , selected: false },
        { text: "Orders", value: "orders", selected: false  }
    ];

    $scope.data = {
        clientSide: 'appointments'
    };

    $scope.goalTypeChanged = function(item) {
        console.log("Selected goalType, text:", item.text, "value:", item.value);
    };

答案 1 :(得分:1)

$scope.data = { clientSide: 'ng' };中的值与$scope.goalTypeList中的任何值都不匹配。

如果$scope.data上的clientSide值是拨号,对话,约会或订单,那么单选按钮应该加载一个选中的单选按钮。

我希望这会有所帮助。

答案 2 :(得分:0)

就像@马克·哈里(Marc Harry)一样,您的ng-value应该等于ng-model。要使其更具动态性(例如,您从后端获得一个对象,并且所选值可能会更改),您可以执行以下操作:

<div class="list">

      <ion-radio ng-repeat="item in goalTypeList"
                 ng-value="item.value"
                 ng-checked="item.selected"
                 ng-model="data.clientSide">
          {{ item.text }}
      </ion-radio>

</div> 

.controller('SettingsCtrl', function($scope, $ionicLoading) {

    $scope.goalTypeList = [
        { text: "Dials", value: "dials", selected: true },
        { text: "Conversations", value: "conversations" , selected: false  },
        { text: "Appointments", value: "appointments" , selected: false },
        { text: "Orders", value: "orders", selected: false  }
    ];

    $scope.data = {
        clientSide: selectedValue()
    };

    function selectedValue = function() {
        for(let i = 0; i < $scope.goalTypeList.length; i++) {
            if ($scope.goalTypeList[i].selected) {
                return $scope.goalTypeList[i].value;
            }
        }
    };

答案 3 :(得分:0)

您还可以将ion-select与ionChange事件一起使用:

v-once

在您的typeScript中:

 <ion-select (ionChange)="checkValue($event)"  interface="popover" 
  placeholder="Select One" >
    <ion-select-option *ngFor="let item of userData" [value]="item"> 
   {{item.optionA}}</ion-select-option>
</ion-select>