最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS: Concatenating Angular variables in HTML attributes - Stack Overflow

programmeradmin0浏览0评论

I'm new to AngularJS, but from what I've seen & done so far it is amazing. What I want to do is have an AngularJS binding inside an HTML attribute, but concatenate it with another string. The main place I would do this is classes & id's, as I like to have names like "thisform" & "thisdivid" etc. An example element from my page is:

<input type="checkbox" 
  name="tdl_task[]" 
  data-ng-checked="list.default" 
  id="tdl_task_{{ id }}" 
  data-ng-class="{tdl_item: true}" 
  data-ng-true-value="done" 
  data-ng-false-value="not_done" />

I would like it to be something like:

<input type="checkbox" 
  name="tdl_task[]" 
  data-ng-checked="list.default" 
  id="tdl_task_" + {{ id }} + "" 
  data-ng-class="{tdl_item: true}" 
  data-ng-true-value="done" 
  data-ng-false-value="not_done" />

but without the plusses. I would like to do this without wrapping it in JavaScript or PHP or creating another whole binding in the controller just for that attribute.

I'm new to AngularJS, but from what I've seen & done so far it is amazing. What I want to do is have an AngularJS binding inside an HTML attribute, but concatenate it with another string. The main place I would do this is classes & id's, as I like to have names like "thisform" & "thisdivid" etc. An example element from my page is:

<input type="checkbox" 
  name="tdl_task[]" 
  data-ng-checked="list.default" 
  id="tdl_task_{{ id }}" 
  data-ng-class="{tdl_item: true}" 
  data-ng-true-value="done" 
  data-ng-false-value="not_done" />

I would like it to be something like:

<input type="checkbox" 
  name="tdl_task[]" 
  data-ng-checked="list.default" 
  id="tdl_task_" + {{ id }} + "" 
  data-ng-class="{tdl_item: true}" 
  data-ng-true-value="done" 
  data-ng-false-value="not_done" />

but without the plusses. I would like to do this without wrapping it in JavaScript or PHP or creating another whole binding in the controller just for that attribute.

Share Improve this question edited Dec 3, 2020 at 21:15 ruffin 17.5k11 gold badges96 silver badges149 bronze badges asked Aug 30, 2013 at 11:58 trysistrysis 8,46619 gold badges53 silver badges88 bronze badges 4
  • 2 If you're using angular, inputs should always have ng-model on them. I'd look at using that instead, as you often don't need id attributes too if you use ng-model correctly. – Michael Low Commented Aug 30, 2013 at 12:19
  • Edited it so it wouldn't be cut off like that. Stack wasn't connecting last night so when it connected again I just submitted whatever was there because I didn't want to type it all out again. – trysis Commented Aug 30, 2013 at 12:44
  • Mikel is right. If you are trying to create an ID then you might be trying to do something un-angularish. Why do you need an ID? Is it for a label for attribute? – checketts Commented Aug 31, 2013 at 7:46
  • 1 Maybe a for attribute. I could also use it for classes, as I said. The point was not to ask why I should or should not do it, but to ask whether & how I can do it. – trysis Commented Aug 31, 2013 at 14:11
Add a ment  | 

2 Answers 2

Reset to default 1

For doing this you have to create one directive. id is provided by html itself you cannot modify its behavior. So create your own custom directive that will take your id and assign this as an id of your html element.

To learn more about directive please visit

Details study on directives

Not sure if this is new in Angular since the question was asked but I had this exact same problem. It turns out that you can get Angular to evaluate + replace the handlebars by preceding it with backslash (), i.e., id="tdl_task_\{{id}}"

发布评论

评论列表(0)

  1. 暂无评论