0

I need to assign for my variable in ES6 string template a style[bold].

My code looks:

Component

export class AppComponent {
  public keyword = '';
  public text = '';
  onKey(value) {
    this.keyword = value;
    this.text = `Search for ${this.keyword} with`;
  };
}

Template

<input #box class="search-for" type="text" (keyup)="onKey(box.value)">

  <p *ngIf="keyword " class="search-line ">{{ text }}</p>

For example, if user types “Angular” then the text will be “Search for Angular with:”

Thank you.

2
  • im not an angular expert, but cant you simply include html into the string? this.text = "Search for <h1>${this.keyword}</h1> with"; Commented Apr 16, 2017 at 10:44
  • @Jonasw thank you, it's very cool and simple idea. thx. Anyway, i have a lot ways to solute this. First idea was with es5 string template, and i interested in solve this problem. But thank you) Commented Apr 16, 2017 at 12:15

1 Answer 1

3

ngModel and Input, that's what you need. Try this code:

import { Input } from '@angular/core';

export class AppComponent {
  @Input() keyword: string;
}

<input class="search-for" type="text" [(ngModel)]="keyword">

<p *ngIf="keyword" class="search-line ">Search for <b>{{keyword}}</b> with:</p>
Sign up to request clarification or add additional context in comments.

4 Comments

Thank, anyway, it's simple solution and it's pretty good. But i don't understand for what @Input() this ;))) But okay.
You can read about directives here and if my answer is what you were looking for, then check it as answer.
but your code works without @Input() :))) All happens in one component ;) And tip for you - you should to use name=".." when using [(ngModel)]=".."
That's not necessary to specify name attribute. But if you want to use that in form and send data through it, then yes, name is needed.

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.