Angular 5 个模型在事件完成后更新

Angular 5 model updates after events completes

问题

我正在使用 angular 5 并构建了一个具有 3 个控件的组件

<ss-multiselect-dropdown id="type" formControlName="type" (ngModelChange)="onChange('type')"></ss-multiselect-dropdown>

<input id="caption" type='text' formControlName='caption' (input)="onChange('caption')" />

<custom-uploader formControlName="url" (input)="onChange('url')"></custom-uploader>

当从 ss-multiselect-dropdowninput 调用 onChange 时 模型在逐步执行 onChange 方法时显示预期值。但是当 onChangecustom-uploader 调用时,旧值(在调用 onChange 之前存在的值)仍然应用于模型。

要明确:

我指的是预期值 select 或该控件的输入

我试过改变

<custom-uploader formControlName="url" (input)="onChange('url')"></custom-uploader>

<custom-uploader formControlName="url" (change)="onChange('url')"></custom-uploader>

<custom-uploader formControlName="url" (ngModelChange)="onChange('url')"></custom-uploader>

似乎模型在 onChange 完成后得到更新。我做错了什么?

代码

自定义-uploader.component.html

<div class="form-group">
    <div class="input-group file-upload">
        <input type="file" (change)="fileChange(input)" #input class="file-upload-btn"/>
        <input type="text" class="form-control" placeholder="Choose a file..." value="{{file}}">
        <i class="fa fa-times delete-file" (click)="removeFile()" *ngIf="file"></i>
        <span class="input-group-btn">
            <button class="btn btn-primary" type="button"><i class="fa fa-upload"></i></button>
        </span>
    </div>
</div>

自定义-uploader.component.ts

import { Component, ViewEncapsulation, ChangeDetectorRef, ViewChild, Output, EventEmitter, forwardRef, SimpleChanges, OnChanges } from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor, NG_VALIDATORS } from '@angular/forms';

import { Ng2ImgMaxService } from 'ng2-img-max';

@Component({
  selector: 'custom-uploader',
  encapsulation: ViewEncapsulation.None,
  templateUrl: './file-uploader.component.html',
  styleUrls: ['./file-uploader.component.scss'],
  providers: [ 
    { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => FileUploaderComponent), multi: true }
  ]
})
export class FileUploaderComponent implements OnChanges, ControlValueAccessor  {

    @ViewChild('input') fileUploaderInput: any;

    public file:any;
    public propagateChange = (_: any) => {};

    constructor(
        private _changeDetectorRef: ChangeDetectorRef,
        private _ng2ImgMaxService: Ng2ImgMaxService
    ) { }   

    writeValue(obj: any): void 
    { 
        this.file = obj;
        this._changeDetectorRef.detectChanges();
    }
    registerOnChange(fn: any): void {
        this.propagateChange = fn;
    }
    registerOnTouched(fn: any): void { }
    setDisabledState?(isDisabled: boolean): void { }
    ngOnChanges(changes: SimpleChanges): void { }

    fileChange(input){
        const reader = new FileReader();
        if (input.files.length) {       
            this.file = input.files[0].name;     
            this.propagateChange(this.file);   
            this.fileUploaderInput.nativeElement.value = "";    
        }
    }

    removeFile():void{
        this.file = '';
        this.propagateChange(this.file);       
    }

}

(change) 这样的括号语法指的是 EventEmitter 在值更改时发出事件。您必须像这样在 FileUploaderComponent 中自己实现它:

@Output() change = new EventEmitter<string>();
...
fileChange(input){
    const reader = new FileReader();
    if (input.files.length) {
        this.file = input.files[0].name;
        this.change.next(this.file);
        ...  
    }
}

然后在实例化它的地方:

模板:

<custom-uploader formControlName="url" (change)="onChange($event)"></custom-uploader>

打字稿:

onChange(fileName: string) {
    //Whatever you want to do with the fileName
}