Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Provided an HTML element of type div, how to set the value of its id attribute, which is the concatenation of a scope variable and a string ?

share|improve this question
up vote 235 down vote accepted

ngAttr directive can totally be of help here, as introduced in the official documentation

https://docs.angularjs.org/guide/directive#-ngattr-attribute-bindings
Update: documentation moved to: https://docs.angularjs.org/guide/interpolation#-ngattr-for-binding-to-arbitrary-attributes

For instance, to set the id attribute value of a div element, so that it contains an index, a view fragment might contain

<div ng-attr-id="{{ 'object-' + myScopeObject.index }}"></div>

which would get interpolated to

<div id="object-1"></div>
share|improve this answer
    
Where does myScopeObject come from? Where would I define it? – Jan Aagaard Oct 6 '14 at 8:26
1  
@JanAagaard Let us assume myScopeObject is a property of a scope object exposed using a controller. Please see also docs.angularjs.org/guide/controller. Is that clear enough for you or shall I elaborate further? – Thierry Marianne Oct 6 '14 at 13:03
    
i did ng-attr-id="{{ 'Panel' + file.Id }}" but it does not generate id="Panel12312" for me :( – Manuel Maestrini May 23 '15 at 5:47
10  
Aren't the following two identical in behavior: <div id="{{ 'object' + index }}"> and <div ng-attr-id="{{ 'object' + index }}"> ? The docs seem to say prepending ng-attr- is to help out for cases where the element is something non-standard, like not a <div>. Am I reading the docs right? – broc.seib Jul 9 '15 at 1:41
    
This did not work for me :( – Vincil Bishop Jun 2 at 23:14

This thing worked for me pretty well:

<div id="{{ 'object-' + $index }}"></div>

share|improve this answer
    
Time flies and perhaps, the most intuitive syntax now just works as expected. I remember having some issues while iterating over a list. – Thierry Marianne Jan 13 '15 at 11:24
    
This is the right answer – Vincil Bishop Jun 2 at 23:14
1  
This will work correct in 90% of cases but you may sometimes get errors which are hard to debug. You should use ng-attr-id instead. – Baki Jun 3 at 13:32

A more elegant way I found to achieve this behaviour is simply:

<div id="{{ 'object-' + myScopeObject.index }}"></div>

For my implementation I wanted each input element in a ng-repeat to each have a unique id to associate the label with. So for an array of objects contained inside myScopeObjects one could do this:

<div ng-repeat="object in myScopeObject">
    <input id="{{object.name + 'Checkbox'}}" type="checkbox">
    <label for="{{object.name + 'Checkbox'}}">{{object.name}}</label>
</div>

Being able to generate unique ids on the fly can be pretty useful when dynamically adding content like this.

share|improve this answer
    
I think this approach has issues. I am initializing the angular binding after the page is loaded. Now at times the div fails to load properly which I guess is because of clash of id of different div. – sumeet Jun 5 '15 at 9:01
    
Interesting. If you could reproduce your behavior in a plunker example I would be happy to check it out. Does using 'ng-attr-id=' work and just using 'id=' not? – Cumulo Nimbus Jun 7 '15 at 23:52

If you use this syntax:

<div ng-attr-id="{{ 'object-' + myScopeObject.index }}"></div>

Angular will render something like:

 <div ng-id="object-1"></div>

However this syntax:

<div id="{{ 'object-' + $index }}"></div>

will generate something like:

<div id="object-1"></div>
share|improve this answer
6  
Explain what you are trying to say? – Kumar Apr 9 '15 at 11:54
3  
Why should ng-attr-id create ng-id..? that's wrong. I wonder who upvotes this – T J Apr 28 at 4:32

You could just simply do the following

In your js

$scope.id = 0;

In your template

<div id="number-{{$scope.id}}"></div>

which will render

<div id="number-0"></div>

It is not necessary to concatenate inside double curly brackets.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.