I’m been struggling for days to dynamically change an attribute of a button with angular binding.
Here is my element
<button [mark]=“mark” (click)=“gotoUser(user?._lat, user?._lon)”>
And when I build I got this error. Can’t bind to ‘mark’ since it isn’t a known property of ‘button’.
This syntax should be perfectly valid in angular and I don’t understand why it’s not working. I try with @Input() as well.
The error disappears if I use [attr.mark] instead, but I cannot change the data after in my component. ( this syntax should be used for conditional attr which it’s not my case I only want to change the value)
Thanks for your answer rapropos. I try to add input on the page component which contain my button. In this case home.ts . It is possible to do like that ? Or I need to create a new component ?
I have different markers on a map with a dynamic popup assign to a marker. So I dynamically set an popup DOM to a marker.
I want to assign an id of marker in the button, so If I click on the button I can get back the marker associated.
So In fact, I just want to update an attribute of the DOM , to get it back later. I can use jquery for that (attr) but I want to do the angular way …
If you want to do it exactly like [mark]="someMark" then you could create a component marked-button whose template is a standard Ionic button, and whose ts file contans a mark field that you can modify and query.
There’s a lot of ways, probably, but I don’t understand your need yet. Why do you need a field other than id? id already exists, no need to do anything fancy to add anything new to the component.
Inside Ionic-Angular, I would create a provider MarkManager, that managed a Map with keys being the buttion IDs, and values being the marks. Then as long as you make sure each button ID is unique, you can assign or change the marks.
But what I would do for real to solve this problem is that I would design a marked-button component in Stencil that extends the Ionic button component. I have the experience with Stencil to do that though, and you probably don’t (at least not yet). Creation of a provider probably involves the least amount of coding, and it’s an official “Angular way” to solve the problem.
As I hinted at above, this can be done with a directive.
@Directive({
selector: "[marked-button]"
})
export class MarkedButton {
@Input() mark: string;
@HostListener('click') onClick() {
alert("i bear the mark of the " + this.mark);
}
}