user2156149 user2156149 - 1 year ago 120
Javascript Question

AngularJS ng-repeat and controllerAs

I using simple syntax to show, hide element in ng-repeat

<div ng-controller='MainController as vm'>
<li ng-repeat="item in vm.items" ng-click="vm.showItem()">
<span ng-show="show">test</span>

When i was using scope everythink worked fine

$scope.showItem = function(){!;

But same code with controllerAs doesn't work

vm = this;
vm.showItem = function(){!;

How can i access to show property of current item in ng-repeat?

Controller as


Answer Source

Get the current this instance from the html like

 <li ng-repeat="item in vm.items" ng-click="vm.showItem(this)">

And use it in your controller

vm.showItem = function(_this){!;

The problem you faced is the change of this context

Updated plunker

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download