Angular Error attaching ng switch inside ng-repeat

Is it possible to insert ng-switch-when the inside of ng-repeat ?

<div ng-controller="UserController"> <div ng-switch on="renderPath"> <div ng-repeat="route in routes"> <div ng-switch-when="route.path"> <div ng-include src="route.url"></div> </div> </div> </div> </div> 

At startup, I received the following exception:

  Error: Argument '?' is required pa@http ://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js:16 ... <!-- ngSwitchWhen: route.path --> 

Thanks!

EDIT

I am trying to do the following

 <div ng-switch on="renderPath"> <div ng-switch-when="path1"> <div ng-include src="url1"></div> </div> <div ng-switch-when="path2"> <div ng-include src="url2"></div> </div> <div ng-switch-when="path3"> <div ng-include src="url3"></div> </div> </div> </div> 

So, I was wondering if I can use ng-repeat to help reduce the redundant HTML code ( ng-switch-when ) above.

+4
source share
2 answers

No, you cannot invest them. ng-switch adds an entry to the $$ watchers array in the ng-controller $ scope. ng-repeat creates a new child region for each iteration. When ng-switch-when occurs, (I guess here), it cannot find any information about the ng switch in the current region of $ (which is now a child region of ng-repeat), so it fails.

Update . I reviewed this one more time. The error occurs with 1.0.3, but not with 1.0.4 (but it does not work with 1.0.4). In fact, what happens is that ng-switch-when scope is a child of the control area and not a child of the ng-repeat iteration. Therefore, route not displayed to ng-switch-when directives. Here's a script that uses 1.0.4 - you can click some links to see the controller areas, ng-repeat and ng-switch-default. When examining the ng-switch-default area, you can see that its parent element $ is the controller area , not the ng-repeat region.

+3
source

I think ng-switch should follow ng-switch-when. In your case you have

<div ng-repeat="route in routes"> between them.

In the above logic, ng-switch is not required or maybe I missed something.

+1
source

All Articles