dimanche 26 mars 2017

Errors when using 'ControllerAs' but works fine when using '$scope' in ng-repeat checkbox

When using '$scope' syntax, checking on individual checkbox correctly outputs its corresponding object name but when applying 'ControllerAs' syntax to same code, checking on individual checkbox abnormally generates error

$scope.users = [{.....}] //using $scope syntax
$scope.selected = [];

$scope.exist = function(item) {
  return $scope.selected.indexOf(item) > -1;
}
$scope.toggleSelection = function(item) {
  var idx = $scope.selected.indexOf(item);
  if (idx > -1) {
    $scope.selected.splice(idx, 1);
  } else {
    $scope.selected.push(item);
  }
}

Representation of above code using in ControllerAs

vm.users = [{....}] //Using 'Controller As' Syntax
vm.selected = [];

vm.exist = function(item) {
  return vm.selected.indexOf(item) > -1;
}
vm.toggleSelection = function(item) {
  var idx = vm.selected.indexOf(item);
  if (idx > -1) {
    vm.selected.splice(idx, 1);
  } else {
    vm.selected.push(item);
  }
}

Error returned in chrome developer tools

TypeError: vm.selected.indexOf is not a function at GridController.vm.exist (gridController.js:37)

Demo Controller As, http://ift.tt/2o7AtWY

Demo $Scope, http://ift.tt/2nYfQQg

Please what could be the issue or could this be a bug when Controller As syntax is applied in this context, thanks




Aucun commentaire:

Enregistrer un commentaire