1

I have in my project a form with an input like this:

@Input()
article: Article;
ngOnInit() {
this.articleForm = this.formBuilder.group({
  title: [ '', Validators.required]
});

form.component.html:

<input class="form-control" formControlName="title"
           type="text" value="{{article ? article.title : ''}}" />

Article is an Object with title attribute and article is an input property.

The Problem is when the input property get an article with a valid title the input value change to the value of the title but strangely steal in invalid state !

Can any one give me an idea of what happening please ?

Thanks in advance.

2
  • First thing, 'value="{{article ? article.title : ''}}"' can be simplified to 'value="{{article?.title}}"' Commented May 24, 2017 at 17:13
  • I like to ensure the readability, thank you any way. Commented May 24, 2017 at 17:32

1 Answer 1

1

This is because the reactive form doesn't register the value of value. If you use one-way-binding your form control will register it:

<input formControlName="title" [ngModel]="article?.title" />

Notice the safe navigation operator here, so that it will not throw error if there is no value.

Sign up to request clarification or add additional context in comments.

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.