Values not passed correctly from parent to child
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKPz5qhCkKGdpPJ0wkSm5JqlliSJoZg92-H4DuCruovhkVFuXerCphZt9wys_Gr3uwQyXObMHDwattjG5BSeCEl_LdDq1GPLct8qYyRyX4fZ9C93oW-TX7IGZLYDwqRgfFCXA4aUxkBAcS/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6VebLk98DYwS3K2nWZSCatab-q4HrSuRk91M4fQA8SHrKU_-x-lq1n780pSU_bIVLqMRv6pc3dhhh-Y2PUwBS9mM0BxvY1eM6XsM-e7Diu4z-wAoeDG0vB3-2_1U9RE8sO0lg4PHiIXbI/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
Values not passed correctly from parent to child
I am new to Angular2 and for practice purposes I have created a set of 3 input components with a Reset button that erases the values entered. I wanted to test the @Input decorator and how values are passed from parent to children. The issue in my code is that the button erases the values only once
Here is the code, the parent component:
import Component from '@angular/core';
@Component(
selector: 'app-root',
template: `
<button (click)="reset()">Reset Age</button>
<app-form-user [age]="age" [name]="name" [surname]="surname">
</app-form-user>`,
styleUrls: ['./app.component.css']
)
export class AppComponent
name;
surname;
age;
reset()
this.name = '';
this.surname = '';
this.age = 18;
and the app-form-user component:
import Component, Input, OnInit from '@angular/core';
@Component(
selector: 'app-form-user',
template: `
<form>
<div>
<label>Name: </label>
<input type="text" value=name/>
</div>
<div>
<label>Surname: </label>
<input type="text" value=surname/>
</div>
<div>
<label>Age: </label>
<input type="number" value=age min="18" max="100"/>
</div>
</form>`,
styleUrls: ['./form-user.component.css']
)
export class FormUserComponent implements OnInit
constructor()
@Input() age;
@Input() name;
@Input() surname;
ngOnInit()
this.age = 18;
2 Answers
2
Angular provide directive to bind some variable to input elements. I don't know why your reset button only works one time, but you should look at the [(ngModel)]
directive from the FormsModule
.
[(ngModel)]
FormsModule
See Two-way data binding with ngModel
<form>
<div>
<label>Name: </label>
<input type="text" [(ngModel)]="name"/>
</div>
<div>
<label>Surname: </label>
<input type="text" [(ngModel)]="surname"/>
</div>
<div>
<label>Age: </label>
<input type="number" [(ngModel)]="age" min="18" max="100"/>
</div>
</form>
And don't forget to import FormsModule
in your component module.
FormsModule
The
@inputs
"age", "name" and "surname" seems to be literal, so I don't think there is reference issue. I think the issue came from the way the value are binded to the HTML input element, with simple one way binding– Noémi Salaün
Sep 8 '17 at 15:38
@inputs
I thought about that as well. Maybe you're right. I remember somewhere in the docs it says something like, that theres a difference between properties and attributes and the way its handled by the dom.
– fubbe
Sep 8 '17 at 15:39
You should bind changes in the parent component to the child component:
Parent HTML:
<app-form-user [(name)]="name"></app-form-user>
Child HTML:
<input type="text" [ngModel]= "name" (ngModelChange)="onModelChange($event)"/>
Child class:
@Input() name;
@Output() nameChange = new EventEmitter();
constructor()
onModelChange(event)
this.nameChange.emit(event);
Working Demo
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Maybe the reference to the values provided to the component change on reset() and thats why it only works once?
– fubbe
Sep 8 '17 at 15:33