ngrepeat alias as and controller as

1.7k views Asked by At

I get this error https://code.angularjs.org/1.3.16/docs/error/ngRepeat/badident when I try to use ngRepeat alias as syntax with controller as syntax:

<li ng-repeat="item in vm.items | filter:vm.searchString as vm.collections">{{item}}</li>

It's not allowed or I do something wrong?

3

There are 3 answers

2
rob On BEST ANSWER

if you need to store the result on the vm variable you can do it like this

<li ng-repeat="item in vm.collections = (vm.items | filter:vm.searchString)">{{item}}</li>

If not then Donal's solution will work

0
Donal On

Should be a simple identifier (such that you could declare it with var {name})

<li ng-repeat="item in vm.items | filter:searchString as collections">{{item}}</li>
0
Fred On

I figured out why I was getting a similar issue.

Angular documentation isn't explicit about this... But if you're going to use Controller As syntax, you MUST set your model values to "this" on your controller. You CANNOT set the values to $scope.

When you inspect the $scope after you alias, you can see a new property on your parent $scope with the name of your alias.

My alias was pmt. I changed my properties and functions from

$scope.myProperty  

to

var vm = this;
vm.myProperty

When you inspect the $scope.$parent of my aliased controller, you see vm listed on it's properties. That's how they do aliasing.

Really wish the documentation was explicit on this.