使用bootstrap ui单选按钮时,在控制器中获取ng-model值

时间:2017-05-26 09:22:35

标签: angularjs angular-ui-bootstrap angularjs-ng-model angularjs-ng-change

我似乎无法将我的单选按钮的值恢复到控制器中......

HTML

<div class="btn-group-wrap">
  <div class="btn-group listBtn text-center">
    <label uib-btn-radio="0" ng-change="newDataset()" ng-model="changeDataset" class="btn btn-default">0</label>
    <label uib-btn-radio="1" ng-change="newDataset()" ng-model="changeDataset" class="btn btn-default">1</label>
    <label uib-btn-radio="undefined" ng-change="newDataset()" ng-model="changeDataset" class="btn btn-default">undefined</label>
  </div>
</div>
<p>filter {{changeDataset}}</p> //i see the change here!!

CONTROLLER

$scope.changeDataset = undefined


$scope.newDataset = function () {
  console.log('new dataset function called');
  console.log('the dataset= '+$scope.changeDataset) //but it just returns undefined here!!!
  console.log('the vacated= '+$scope.filter.vacated)
};

它识别该函数正在运行,但它始终只记录undefined

2 个答案:

答案 0 :(得分:0)

您的代码有效,// Output for non-matching characters: 000, 666, 900, 901, 902, 903, 904, 905, 906, 907, 908, ... and so on 出现在控制台中,因为您尝试记录undefined,其设置为$scope.filter.vacated

以下是您的代码的代码段演示:

undefined
angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('ButtonsCtrl', function ($scope) {
  $scope.filter = {
    vacated: undefined
  };
  
  $scope.newDataset = function () {
    console.log('new dataset function called');
    console.log('the dataset= ' + $scope.changeDataset);
    console.log('the vacated= ' + $scope.filter.vacated);
  };
});

答案 1 :(得分:-1)

我通过将ng-model="changeDataset"更改为ng-model="$parent.changeDataset"

来解决此问题

$ parent - 映射到包含当前范围的范围