How to Set the Id Attribute of a HTML Element Dynamically with Angularjs (1.X)

How to set the id attribute of a HTML element dynamically with angularjs (1.x)?

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

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>

How to set a dynamic id HTML attribute of an angular component?

So I found a solution to this problem. The problem was twofold:

  1. The first problem was related to the requirement of having a genuine id attribute in contrast to the attribute name prefixed with ng-reflect- by angular.

    What I didn't know is that from Angular 4, this prefix gets added to any attribute which is declared as an @Input variable for the component in development mode (for an in-depth explanation see this post).

    The solution to this problem was to write [attr.id]=chartId instead of [id]=chartId. The reason is that in this case I needed to set an HTML Attribute, however using the square brackets notation I created a Property Binding. In order to dynamically set an HTML Attribute via the property binding syntax you have to add the prefix attr. to the attribute's name.

    For a good overview regarding all valid binding syntaxes see the Angular Docs.
    Specifically to review how to do attribute binding see this section.

  2. The second problem was that I tried to access the id attribute of the component when it has not yet been created. This problem is related to the lifecycle hooks of Angular, where there are different stages. For a good overview of Angular's lifecycle hooks, see this page.

    Because I didn't know it myself in detail, I will elaborate it here shortly.

    Angular comes with a fixed set of lifecycle hooks which are called each time a component is set up and are also called in a predefined order. The hooks are defined as follows:

    1. constructor
    2. ngOnChanges
    3. ngOnInit
    4. ngDoCheck

      1. ngAfterContentInit
      2. ngAfterContentChecked
      3. ngAfterViewInit
      4. ngAfterViewChecked
    5. ngOnDestroy

    _

    Important to note is that elements created or prepared in one step can only be accessed in any of the subsequent steps. In my case I put all the code for the initialization of the chart into the ngAfterContentInit hook, however the component is only fully loaded when ngAfterViewInit is called.

    Finally the solution was to put the pre-initialization code into the constructor and to place the chart initialization code into the ngAfterViewInit method. At this stage the id attribute had been created properly and could be accessed by the chart.

AngularJS dynamically assign IDs to elements in a dynamic table

You can dynamically add IDs to the table like so:

 <tr ng-repeat="x in tableArray track by $index">
<td id="redCorner{{$index}}">Red Corner</td>
<td>Round {{$index}}</td>
<td id="blueCorner{{$index}}">Blue Corner</td>
</tr>

This should give you unique red and blue corner IDs for each row of the table in the form of redCorner0, blueCorner0, redCorner1.... Let me know if this helps.

Angularjs dynamically set attribute

You need to recompile your div

var el = angular.element("div_id");
$scope = el.scope();
$injector = el.injector();
$injector.invoke(function($compile){
$compile(el)($scope)
})

http://jsfiddle.net/r2vb1ahy/

Dynamically set attribute for child directive in Angularjs

You should be able to link the attribute to a scope variable. If you are talking about designing the directive yourself, you can define the controller's scope variables as "=" and they will link bidirectionally to the value passed to the attribute. Check the AngularJS: Developer Guide for more info.

You might also be able to assign the value of the attribute using {{...}} annotation to inject a scope variable directly into the markup.

Handling DOM element with dynamically generated id

As I heard and read, it is a problem with AngularJs. The controller codes, execute before DOM complete, so without $timeout, there is no id '#'+$scope.ids[0] to select. You must use $timeout to select and use that.

For more information search and read about digest cycle of angularjs.



Related Topics



Leave a reply



Submit