0

Within a ng-repeat I have a ng-switch conditional. I want the value of the ng-switch argument to be a binding. However the curly-bracket binding doesn't seem to be converted into the expected generated value. I'm aware that ng-switch creates a new scope, and that is surely causing the problem. I know about a 'dot' work-around but as the values here are coming from a service via a controller query, I can't figure out how to implement it in this situation.

html

<tr ng-repeat="user in users">
    <td ng-switch on="editState"><p ng-switch-when="noEdit">{{user.username}}</p><input type="text" value="{{user.username}}" ng-switch-when="{{user.username}}"></td>
</tr>

controller.js

$scope.users = Users.query();

-edit-

this is the function that triggers the switch

$scope.editUser = function(usernameEdit) {
    $scope.editState = usernameEdit;
};
3
  • Can you explain what you are trying to do here: ng-switch-when="{{user.username}}"? Can you instead use ng-switch-default? Commented Apr 8, 2013 at 21:34
  • This table row is to be repeated for each user. When I press the 'edit' link next to each user i want the switch conditional to match just that username and so hide the paragraph and show the text input. If I used switch default wouldn't all the users in the list switch? Commented Apr 9, 2013 at 4:46
  • I've also added the function that triggers the switch argument Commented Apr 9, 2013 at 5:54

1 Answer 1

2

A single $scope.editState won't work if you want to be able to edit multiple users at the same time. I suggest putting an edit property on each user, and use that property to control the ng-switch:

<tr ng-repeat="user in users">
   <td ng-switch on="user.edit"> 
      <span ng-switch-when="true">
         <input  type="text" value="{{user.username}}">
         <a href="" ng-click="user.edit=false">done</a>
      </span>
      <p ng-switch-default>
         <a href="" ng-click="user.edit=true">edit</a> {{user.username}}
      </p>
   </td>
</tr>

Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant thanks for making it so clear, it seems I didn't really understand ng-switch from the angular docs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.