2

Consider I have a few objects, each of them having the properties x, y and z.

Now consider I have the following Angular ng-repeat:

<div ng-repeat="obj in objs">
    Values are: {{ obj.x }}, {{ obj.y }} and {{ obj.z }}
</div>

Is there any way I can implicitly use the obj variable, so I could do:

<div ng-repeat="obj in objs">
    Values are: {{ x }}, {{ y }} and {{ z }}
</div>

2 Answers 2

2

What you're trying to do doesn't seem to be a good idea, as per this answer to a previous question. Basically, because... (switch "block" for "obj")

(...) all primitives in block (strings, ints, etc) would just be copied from the block object to the block scope object (...)

That's the basic reason for directives like ng-repeat making a new scope of their own.

I see your point in trying to save some keystrokes, but it's not like you can't just copy & paste ;)

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

11 Comments

I haven't said anything about using that as a directive template. Actually, I'm going to compile this against an array of objects, so the only variable the template would see is that object, nothing more.
Regardless of being a directive template or not, it still is not a good idea: the risk of running into conflicts and other issues is too high just to save "a couple" of keystrokes. If you still want to take the risk, I would probably give ng-init a try...
I really can't see those risks. Care to explain a little bit further?
From github.com/angular/angular.js/wiki/…: "If item is a primitive (as in myArrayOfPrimitives), essentially a copy of the value is assigned to the new child scope property. Changing the child scope property's value (i.e., using ng-model, hence child scope property num) does not change the array the parent scope references."
@alexandernst, this is absolutely correct. Original ng-repeat had just such an option, but broke in various use cases. For example, in nested repeats, you would struggle to specify your focus object.
|
2

If you were to have it in this format:

<div ng-repeat="obj in objs">
    Values are: {{ x }}, {{ y }} and {{ z }}
</div>

Then that would mean that x, y, z are variables on the scope. So that conflict prevents you from having an implicit variable like that, except if you actually put that variable on the scope through a helper function, but! that would create overlap between scope variables having the same name per each object in the array, so either way you have a naming conflict.

Sorry, I usually don't do proofs, but hopefully this can prove by contradiction why that would be impossible. Hope that helps!

1 Comment

I haven't said anything about using that as a directive template. Actually, I'm going to compile this against an array of objects, so the only variable the template would see is that object, nothing more.

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.