Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use ng-attr?

Tags:

It seems like using direct attributes and the ng-attr-* directive do the same thing. For example, the following render equivalently:

   <div ng-attr-id="{{ 'object-' + value }}">ID</div>
   <div id="{{ 'object-' + value }}">ID</div>

When should I use ng-attr-* and when should I use the direct HTML attribute?

like image 920
zloctb Avatar asked Sep 01 '15 13:09

zloctb


People also ask

What is Ng ATTR?

ng-attr is used for add or not the attribute in context. If the expression {{undefined || null}} , the attribute is not added otherwise if has a value then attribute is added with the value {{ value }} . The most common cases is in interpolation.

How to bind data attribute in Angular?

We simply use attribute binding to add and set a value for a data attribute. According to Angular docs: Attribute binding syntax resembles property binding. Instead of an element property between brackets, start with the prefix attr, followed by a dot (.)

What is interpolation in Angular js?

String Interpolation in Angular 8 is a one-way data-binding technique that is used to transfer the data from a TypeScript code to an HTML template (view). It uses the template expression in double curly braces to display the data from the component to the view.


2 Answers

ng-attr is used for add or not the attribute in context. If the expression {{undefined || null}}, the attribute is not added otherwise if has a value then attribute is added with the value {{ value }}. The most common cases is in interpolation. Related: Conditionally adding data-attribute in Angular directive template

like image 85
Sieg Avatar answered Sep 22 '22 00:09

Sieg


You use them for custom html data attributes - like if you wanted an attribute of let's say myData you would do

  <div ng-attr-myData="{{ 'object-' + value }}">ID</div>
like image 30
ajmajmajma Avatar answered Sep 19 '22 00:09

ajmajmajma