What I'm using
What I'm trying to do
What I have
My HTML List
A button hooked up to a click event
What I'm not sure how to do
[phantomOp]="myItems" [myOper]="oper"
HTML
<div class="container">
<ul>
<li> Albums </li>
<li> Dates </li>
</ul>
</div>
<button (click)="addContainerAttributes"> Add Container Attributes </button>
What I would like the HTML to look like after a button click
<div class="container" [phantomOp]="myItems" [myOper]="oper">
<ul>
<li> Albums </li>
<li> Dates </li>
</ul>
</div>
TS
addContainerAttributes(){
// Not entirely sure what to put here
}
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.
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 (.)
Attribute binding syntax is like property binding. In property binding, we only specify the element between brackets. But in the case of attribute binding, it starts with the prefix attar, followed by a dot (.), and the name of the attribute.
Dynamic attributes represent changing characteristics of the resource. Dynamic attributes of a host resource, for example, would identify such things as the average number of processes that are waiting in the run queue, processor idle time, and the number of users who are currently logged on.
Adding Angular specific markup added dynamically at runtime will just be ignored by Angular.
Bindings like [phantomOp]="myItems" [myOper]="oper"
are processed by Angular only when a component is compiled. Normally (with AoT) this is before you deploy your application.
How can I use/create dynamic template to compile dynamic Component with Angular 2.0? explains how to compile a component at runtime.
You can just introduce a field in your component
isClicked:boolean = false;
add this to your element statically
[phantomOp]="isClicked ? myItems : null" [myOper]="isClicked ? oper : null"
and in the click handler
(click)="isClicked=true"
I ran into a similar situation recently. I wanted to dynamically add and remove the capture
attribute on a FileInput. Now just the presence of the capture
attribute and the File Input will fall to capture mode, so having the value as null or '' did not work for me.
This is how I finally got it to work. I used @ViewChild
and ElementRef
. In your case it would be something like:
<div #yourContainer class="container">
<ul>
<li> Albums </li>
<li> Dates </li>
</ul>
</div>
<button (click)="addContainerAttributes"> Add Container Attributes </button>
Then in the TS
import { Component, ElementRef } from '@angular/core';
@Component({
selector: 'app-your-component',
templateUrl: './your-component.component.html',
styleUrls: ['./your-component.component.css']
})
export class YourComponent {
@ViewChild('yourContainer', {static:true}) yourContainer: ElementRef;
....
addContainerAttributes(){
// Not entirely sure what to put here
this.yourContainer.nativeElement.setAttribute('phantomOp', myItems);
this.yourContainer.nativeElement.setAttribute('myOper', oper);
}
// if you wish to completely remove the attribute dynamically
removeContainerAttributes(){
this.yourContainer.nativeElement.removeAttribute('phantomOp');
this.yourContainer.nativeElement.removeAttribute('myOper');
}
}
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