2

I have some HTML:

<div class='list-group' >
    <a href="javascript:;" ng-attr-id="{{company.policy_number}}" class='list-group-item' ng-repeat="company in companies">{{company.primary_name}}</a>
</div>

Each object looks like:

{
    policy_number: 12345,
    primary_name: "Policy Name Here"
}

How can I set the id of the link that I create to the policy number of the object?

I have tried both id="{{company.policy_number}} and ng-attr-id={{company.policy_number}}, is there something that I am missing here where I can not access this?

Thanks!

2
  • 1
    id={{company.policy_number}} should work, are you getting an error or is the id field empty? Commented Nov 10, 2014 at 16:03
  • No, it just makes it id={company.policy_number}. If I make it id={company.policy_number},though, it is blank. Commented Nov 10, 2014 at 16:05

2 Answers 2

2

You should use id="{{company.policy_number}}".

It is working in this plunker

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

5 Comments

Hmm, ya that's weird... Even if I use id="{{company.primary_name}}" it still just shows id="{company.primary_name}". But I know primary_name is working.
which angular version you are using.
in my case this version work perfect.if is it possible to you then please create plunker or post more detailed code.
Do you think it has anything to do with me using XSLT? This is actually part of a .xsl file, I wonder if the transformation is replacing one of the sets of curly braces.
That was it, here is a link to a fix: stackoverflow.com/questions/23030507/…
0

I don't like it myself, when someone instead of answering the question replies with "why do you even doing that, you should be doing it another way", but nevertheless here I go.

If you are looking to give ID to controls within ng-repeat loop, you are likely doing something wrong. You only need ID if you are accessing DOM, but the whole point of angular is that you should not be accessing DOM. Once you see jquery in your controller, it's time to stop and reevaluate why are you doing it and there's pretty much a guaranteed better way to handle what you want.

For example: if you are looking to provide a click handler it is much better to have something like a ng-click="policy_clicked(company.policy_number)" inside your HTML within a loop

Again, I apologize for giving you a non-answer, but I've seen folks doing this mistake over and over again, and it leads to hugely unsustainable code

1 Comment

I understand that, this application was already written, I am just adding some functionality to it using angular. Trust me, if I could rewrite this entire thing I would. Thanks for the advice though :)

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.