事件完成后Angular 5模型更新

时间:2018-07-02 22:49:44

标签: angular angular5 angular-reactive-forms

问题

我正在使用角度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>

onChangess-multiselect-dropdown调用input时 逐步执行onChange方法时,模型将显示期望值。但是,当onChange调用custom-uploader时,旧值(调用onChange之前的值)仍会应用于模型。

要明确:

“期望值”是指我为该控件选择或输入的值

我尝试更改

<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完成后,模型已更新。我做错了什么?

代码

custom-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>

custom-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);       
    }

}

1 个答案:

答案 0 :(得分:1)

(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
}

TypeScript:

s.ios.deployment_target = '9.0'
s.pod_target_xcconfig = { 'SWIFT_VERSION' => '4.1' }
s.source_files = 'CustomPod/Classes/*.swift'
s.resources = 'CustomPod/Frameworks/Privateframework.bundle'
s.vendored_frameworks = 'CustomPod/Frameworks/Privateframework. framework'
s.xcconfig = { 'FRAMEWORK_SEARCH_PATHS' => '$(SYSTEM_LIBRARY_DIR)/CustomPod/Frameworks/' }