Reputation: 151
I'm using Angular and want to change the default color of an <input type="color">
element. Currently, it shows black (#000) as the default, but I want to set it to a different color. How can I achieve this?
Also, I'm using Angular v18. I've tried setting the value attribute, but it doesn't work.
My code snippet:
<input type="color" name="color" value="#ff0000" ngModel />
Upvotes: 0
Views: 51
Reputation: 57986
You should use either use [value]
or [(ngModel)]
but not both together, that is causing the bug.
<input type="color" name="color" [(ngModel)]="model"/>
<input type="color" name="color" [value]="model"/>
import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-root',
standalone: true,
imports: [FormsModule],
template: `
<input type="color" name="color" [(ngModel)]="model"/>
<input type="color" name="color" [value]="model"/>
`,
})
export class App {
model = '#ff0000';
}
bootstrapApplication(App);
Upvotes: 2
Reputation: 26
<input type="color" name="color" [(ngModel)]="selectedColor" value="#ff0000" />
export class AppComponent {
selectedColor: string = '#ff0000'; // default color red
}
Upvotes: 1