0

The title is pretty self-explanatory, so - I put an input element in app.component.html file, this is the code: <input type="text" name="titleInput" id="titleInput" [(ngModel)]="titleInput"> Then i tried making an h1 element in another component, as I wanted it to display what was typed in the input field. Didn't work. Made an h1 element in app.component - works fine. I searched a lot, but didn't find anything. Can you help me?

Raptrozz
  • 3
  • 5

2 Answers2

0

You can use your model titleInput which is manipulated through the input to display, in your case, in your header tag using interpolation, i.e. {{.}}.

For example:

<div>
    <h1>{{titleInput}}</h1>
    <input type="text" name="titleInput" id="titleInput" [(ngModel)]="titleInput"/>
</div>

You can see it working here.

Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
  • In this example i can see it works, but it works in the same component. And i'm trying to use string interpolation, but the h1 element is in another component, and then it doesn't. – Raptrozz Aug 29 '17 at 11:17
0

There are two ways.

Either do it with a child parent relationship with @Input. https://angular.io/guide/component-interaction (see other post).

Or use LocalStorage.setItem('titleInput', titleInput); in first component.

And in second LocalStorage.getItem('titleInput'); and LocalStorage.removeItem('titleInput');

Swoox
  • 3,707
  • 2
  • 21
  • 32