1

Say I have

<div ng-repeat="i in items">
  <span ng-show="editing">i.something</span>
  <span ng-show="!editing">i.something</span>
  <a ng-click="dosomething(i)">click</a>
</div>

in the controller I have:

$scope.dosomething = function (model){
   //do stuff
   editing = false;
}

How can I access the variable of only the iteration of the ng-repeat, i've tried passing editing through as a parameter to dosomething i've tried accessing it through $scope.editing, I've been looking through $rootScope documentation. Can't work it out.

I acknowledge I could add editing as a parameter of i but I'd rather not touch the model unnecessarily if there is a way to access each scope respectively without changing editing for the other iterations.

1
  • which type of data your items does contain? on which condition you want show your which data? your //do stuff part here must contain condition on which you can set editing to true n false. but I'm confused with your question so can't provide you a solution. be more clear with what is to be achieved... Commented Jul 1, 2014 at 11:18

3 Answers 3

1

Your dosomething() function is run in the context of the parent scope (thus affects all items).
So, you should remove $scope.editing = false from the body of dosomething().

Then, inside your ngClick expression, you can insert editing=false which will be evaluated in the context of the child-scope created by ngRepeat for each item, and not affect other items.

E.g.:

<div ng-repeat="i in items">
    ...
    <a ng-click="editing=false;dosomething(i)">click</a>
</div>

$scope.dosomething = function (model){
    // Do stuff
    // Don't touch `editing` here
}

See, also, this short demo.

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

2 Comments

Sorry been afk for a week, Thats a feasible solution for other scenarios but that time I think I was trying to change editing based on conditionals inside the called function.
@Kiee: You could return a value and set editing based on that: ng-click="editing=dosomething(i)"
0

the ng-repeat directive creates child scopes and binds them to every "repeat" element. Therefore, you can access your "editing" variable in the child scope this way.

$scope.dosomething = function (model){
   //do stuff
 angular.element(model.currentTarget).scope().editing = flase;
}

Comments

0

If I understand correctly:

<div ng-repeat="i in items">
  <span ng-show="i.editing">i.something</span>
  <span ng-show="!i.editing">i.something</span>
  <a ng-click="dosomething(i)">click</a>
</div>

And in the controller:

$scope.dosomething = function (model){
   //do stuff
   model.editing = false;
}

Where model refers to the ng-repeat item's (i) scope.

Comments

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.