I'm trying to set up a Bootstrap tab strip with Angular 2. I have the tabs rendering in an ngFor
but I'm getting template errors when I try to put the #
infront of the href
expression. So this template compiles but isn't what I want:
<ul class="nav nav-tabs" role="tablist"> <li *ngFor="let aType of resourceTypes; let i = index" [ngClass]="{'active': i == 0}" role="presentation"> <a [attr.href]="aType.Name" [attr.aria-controls]="aType.Name" role="tab" data-toggle="tab"> {{aType.Name}} </a> </li> </ul>
What I want to do is [attr.href]="#aType.Name"
but that blows up. What is the correct syntax to prepend the #
in front of the expression in the attribute directive?
AngularJS ng-href DirectiveThe ng-href directive should be used instead of href if you have AngularJS code inside the href value. The ng-href directive makes sure the link is not broken even if the user clicks the link before AngularJS has evaluated the code.
If you are using routing and want to add a href attribute to a specific route. Use the routerLink directive as it will add the href attribute to <a> tags. Show activity on this post. Show activity on this post.
The attribute directive changes the appearance or behavior of a DOM element. These directives look like regular HTML attributes in templates. The ngModel directive which is used for two-way is an example of an attribute directive.
Attribute binding in Angular helps you set values for attributes directly. With attribute binding, you can improve accessibility, style your application dynamically, and manage multiple CSS classes or styles simultaneously.
There is no need to prefix with #
In this code
<ul class="nav nav-tabs" role="tablist"> <li *ngFor="let aType of resourceTypes; let i = index" [ngClass]="{'active': i == 0}" role="presentation"> <a [attr.href]="aType.Name" [attr.aria-controls]="aType.Name" role="tab" data-toggle="tab"> {{aType.Name}} </a> </li>
aType
already refers to the *ngFor
variable.
If you want to prefix a literal #
you can use
[attr.href]="'#' + aType.Name"
There is also no need for attr.
if the element actually has the property href
which is the case with <a>
.
Well you can bind it with string interpolation:
href = "#{{aType.Name}}"
(Note that the attribute used here is href
, not [attr.href]
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With