I have a form where I am previewing a video youtube before posting it off. I am also using Reactive forms.
I need to be able to let the user preview their video before posting if off and currently, I am using ngModel for this to work.
<iframe *ngIf="video_url; else preview"
[src]="'https://www.youtube.com/embed/' + video_url | safe: 'resourceUrl'" frameborder="0" .allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"allowfullscreen></iframe>
<ng-template #preview>
<div class="preview">
<span>
<p><i class="fab fa-youtube"></i></p>
<p><small>Preview Your Youtube Video Here</small></p>
</span>
</div>
</ng-template>
<input type="text"
formControlName="video_url" id="video_url"
[(ngModel)]="video_url"
placeholder="Add your Video ID here e.g: https://youtube.com/This_Is_Your_Video_ID"
>
I know this isn't best practice as I am now getting these warnings in the console:
It looks like you're using ngModel on the same form field as formControlName. Support for using the ngModel input property and ngModelChange event with reactive form directives has been deprecated in Angular v6 and will be removed in Angular v7.
What is the best way to 'model' existing or new values in forms with Reactive Forms onchange($event)? Or is there a better technique for this?
I say existing and new, as when the form is in edit state, I need to also model the existing video_url which is found at article.video_url. Currently, this works for one approach only and not both existing and new values.
FormControl
s instead ofngModel
. That's why it does not make sense to use bothformControlName
andngModel
. The easiest way to create aFormControl
is to useconst myFormControl = new FormControl()
onts
side and assigning it via<input type="text" [formControl]="myFormControl">
. Then you can subscribe tomyFromControl.valueChanges()
which is anObservable
. – Arnaud DenoyellemyFormControl.value
. Hope it helps. – Arnaud Denoyelle