Angularjs - Ngswitch And Ngclick Not Working In Ngrepeat
I want to display the elements of a list thanks to a ngSwitch but I can't figure out how to do with a ngRepeat. I've begun by doing it without a list, just to understand how ngSwit
Solution 1:
Some problems:
When dealing with angular-directives, you usually don't need to use the
{{...}}
syntax, just use the real values. So instead of:data-ng-click="sw='{{test.name}}'"
use:
data-ng-click="sw = test.name"
(see next point for why this won't be enough)
ng-repeat
uses its own scope with transclusion, so the above will setsw
in the wrong scope, use:data-ng-click="$parent.sw = test.name"
you can't build
ng-switch-when
's withng-repeat
, tryng-show/hide
instead try:<div ng-repeat="test in tests" ng-show="sw == test.name">
demo: http://jsbin.com/uxobot/1/
But all in all, I don't see the need for ng-switch/ng-repeat
on the second div. The following has the same effect and is probably a lot more semantic:
<divng-controller="MyCtrl"><divclass="click"><divng-repeat="test in tests"data-ng-click="$parent.active = test">
{{test.name}}
</div></div><divclass="switch">
{{active.text}}
</div></div>
Post a Comment for "Angularjs - Ngswitch And Ngclick Not Working In Ngrepeat"