Goal: I'm trying to update the "name" attribute of the crisis object in the html template.
Problem: The ngModel directive is not reading the initial crisis.name value into the html-input element and it is also not updating the name when the user edits the input field.
Problem Visualized:
Empty Input field that should be bound to crisis.name
Html template:
<h2>Crisis</h2>
<div *ngIf="crisis$ | async as crisis">
<h3>"{{ crisis.name }}"</h3>
<div>
<label>Id: </label>{{ crisis.id }}</div>
<div>
<label>Name: </label>
<input [(ngModel)]="crisis.name" >
</div>
<p>
<button (click)="goToCrises(hero)">Back</button>
</p>
</div>
Component:
export class CrisisDetailComponent implements OnInit {
// The input decorator simply allows parent components to pass down a value to it
// The parameter itself still acts as a normal attribute of the component
public crisis$: Observable<Crisis>;
constructor(
private router: Router,
private route: ActivatedRoute,
private crisisService: CrisisService
) { }
public ngOnInit(): void {
console.log('detail initialized');
this.crisis$ = this.route.paramMap.pipe(
switchMap((params: ParamMap) => this.crisisService.getCrisis(params.get('id')))
);
}
public goToCrises(crisis: Crisis): void {
const crisisId = crisis ? crisis.id : null;
this.router.navigate(['../', { id: crisisId, foo: 'foo' }], { relativeTo: this.route });
}
}