2

I want to make a simple thing that the user input a color that he choose and make the color begin on some paragraph .

The input and the click button that the user send the inputColor to the typescript :

<form>
    <input #inputColor  type="text"/>
    <button type="button" (click)="changeColorText(inputColor)"></button>
    </form>

The function changeColorText() on the typescript :

export class HelloWorld implements OnInit {
public inputColor:string;


  constructor() { }

  ngOnInit(): void {
  }

  public changeColorText(str:string) :void {
    this.inputColor = str;
  }

}

The problem :

The paragraph change the color if i choose one (Witout input from the user) :

<p style="color:blue">Hello World !</p>

But I want what the user put in the box to make the paragraph like the color that sent, I couldn't find the correct syntax here:

<p style="color:{{inputColor}}">Hello World !</p> // ERROR !!!!

<p style=`color:{{inputColor}}`>Hello World !</p> // No compilation error .. but not working:
4
  • initialize it public inputColor:string=''; Commented May 30, 2020 at 14:46
  • How is it even compiling if you have (click)="changeColorText(inputColor)" and inputColor is not a string but an HTMLElement? Commented May 30, 2020 at 14:49
  • Hey pc_coder , its not working , Balastrong you say the problem is with #inputColor? Commented May 30, 2020 at 14:52
  • I added one answer demo for you and explanation answer. You can check it @Pluto Commented May 30, 2020 at 15:07

2 Answers 2

1

You should use a style binding as following, details can be found at https://angular.io/guide/template-syntax:

<p [style.color]="inputColor" >Hello World !</p>
Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried with:

<form>
    <input #inputColor  type="text"/>
    <button type="button" (click)="changeColorText(inputColor.value)">ClickMe</button>
</form>

<p style="color: {{inputColor.value}}">Hello World !</p>

You have to take the value from inputColor as it's an HTMLElement on this side.


Another option, is to have different names from the element and your string, for example:

<form>
    <input #inputColor  type="text"/>
    <button type="button" (click)="changeColorText(inputColor.value)">ClickMe</button>
</form>

<p style="color: {{inputColorStr}}">Hello World !</p>

With

  public inputColorStr: string;

  public changeColorText(str: string): void {
    this.inputColorStr = str;
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.