Ionic Framework/AngularJS中的两组单选按钮



我有两组具有相同值的单选按钮。其中一个是用户输入,另一个用于显示输入后的结果。

我能够正确地保存值,但问题是只有一个单选按钮在任何时候显示为活动。我真正想要的是为每组设置一个激活的收音机。

app.js:

angular.module('starter', ['ionic', 'starter.controllers'])
.config(function($stateProvider, $urlRouterProvider) {
  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js
  $stateProvider
    // setup an abstract state for the tabs directive
    .state('tab', {
      url: "/tab",
      abstract: true,
      templateUrl: "templates/tabs.html"
    })
    .state('tab.dash', {
      url: '/dash',
      views: {
        'tab-dash': {
          templateUrl: 'templates/tab-dash.html',
          controller: 'DashCtrl'
        }
      }
    })        
  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/tab/dash');
});

controllers.js:

angular.module('starter.controllers', [])
.controller('DashCtrl', function($scope, $window, $ionicPlatform) {
    $ionicPlatform.ready(function() {
        // Ready functions
    });
    $scope.grossOptionsList = [
    { text: "Year", value: "year" },
    { text: "Month", value: "month" },
    { text: "Week", value: "week" },
    { text: "Day", value: "day" }
    ];
    $scope.resultsOptionsList = [
    { text: "Year", value: "year" },
    { text: "Month", value: "month" },
    { text: "Week", value: "week" },
    { text: "Day", value: "day" }
    ];
// Default values
    $scope.data = {};
    $scope.data.grossOptions = 'year';
    $scope.data.resultOptions = 'month';

    $scope.updateGrossOptions = function(item) {
        console.log( 'Gross option: ' + item.value );
    }
    $scope.updateResultOptions = function(item) {
        console.log( 'Results option: ' + item.value );
    }
})

tab-dash.html:

<ion-view title="Salary Calculator">
  <ion-content class="has-header">
    <div class="button-bar padding">
        <ion-radio ng-repeat="item in grossOptionsList"
                   ng-value="item.value"
                   ng-model="data.grossOptions"
                   ng-change="updateGrossOptions(item)"
                   class="button button-positive button-outline">
          {{ item.text }}
        </ion-radio>
        </div>
    <div class="list list-inset">
        <div class="item item-divider">
                Results per {{ data.resultOptions }}
            </div>
        <div class="button-bar padding row item">
        <ion-radio ng-repeat="item in resultsOptionsList"
                   ng-value="item.value"
                   ng-model="data.resultOptions"
                   ng-change="updateResultOptions(item)"
                   class="button button-positive button-outline">
          {{ item.text }}
        </ion-radio>
        </div>
</div>
  </ion-content>
</ion-view>

Demo plnkr在这里显示,我可以为每个单选按钮组设置值,但我想为每个设置一个(即输入月和结果日)。

别介意!我只是没有设置两组的name值。叹息。

只是想张贴我的解决方案在这里使用您的实现,这需要的名称属性,以防它帮助别人。我挣扎了一点,因为我也使用这个与ng-repeat。这个解决方案对我很有效:

<ion-radio ng-repeat="option in question.options" ng-model="question.response" ng-value="option.text"name="{{question.id}}">
   {{option.text}}
</ion-radio>

最新更新