data binding - Dynamically set attribute on component in html template angular 2 -
i trying set attribute on component if expression true. give me example?
what have now:
<div [danger]="inarray(choice.likers, user, id)"></div>
but not compile.
inarray own method returns true or false. see is, if expression returns true, output be
<div danger></div>
in angular 1 there this: ng-attr-... statement did above.
to set attribute use attribute binding:
<div [attr.danger]="inarray(choice.likers, user, id)"></div>
if want show empty attribute conditionally return empty string or null in inarray()
:
inarray() { let value; if (isinarray) { value = ''; } else { value = null; } return value; }
so attribute empty or danger attribute dont exist. result is:
<div danger></div>
Comments
Post a Comment