2017-08-19 1 views
0

Ich möchte Bedingung stellen, wenn ich Formular zu dieser Zeit Datei senden oder speichern Datei ändern Funktion aufrufen andernfalls nicht. Wie setze ich diese Bedingung in den unteren Code?Willst du Bedingung in upload Bild in angular2 setzen

ist diese Formularvorlage für das Hochladen von Bildern:

<form name="form" (ngSubmit)="f.form.valid && save()" #f="ngForm" novalidate enctype="multipart/form-data"> 
     <div> 
      <input type="file" (change)="fileChange($event)" placeholder="Upload file"> 
      <img [src]="employee.profile_image_url" height="50" width="50"> 
     </div> 
</from> 

Es ist eine Form Sparfunktion in der Komponente:

save(): void { 
    if (this.employee.bank_details == null) { 
     this.employeeService.add_bankDetails(this.model, this.employee); 
    } 
    if (this.employee.current_address == null) { 
     this.employeeService.add_addressDetails(this.model, this.employee); 
    } 
    if (this.employee.permanent_address == null) { 
     this.employeeService.add_PaddressDetails(this.modelP, this.employee); 
    } 
    this.employee.full_name = (`${this.employee.first_name} ${this.employee.last_name}`); 
    this.employeeService.update(this.employee); 
} 

Its a filechange Ereignis für das Hochladen von Bildern in der Komponente:

fileChange(event: any) { 
    const imageFolder: string = this.employee.id; 
    const fileList: FileList = event.target.files; 
    const file: File = fileList[0]; 
    const storageRef = firebase.storage().ref().child(`${imageFolder}/profile.jpg`).put(file) 
    .then((snapshot) => { 
    this.employee.profile_image_url = snapshot.downloadURL; 
console.log(snapshot.downloadURL); 
}); 

} 
+0

aus dem Code Ich verstehe, dass Sie filechange() aufrufen, aus dem Änderungsereignis des Eingangs. Nennen Sie es von der Funktion save() Senden ... – Vega

+0

Ja, aber wie können Sie bitte erklären .. – Krunal

Antwort

0

Wie ich verstanden habe, sollte ich folgendes tun:

HTML

<form name="form" (ngSubmit)="f.form.valid && save(filename.value)" #f="ngForm" novalidate enctype="multipart/form-data"> 
    <input #filename type="file" placeholder="Upload file"> 
    <img *ngIf='isSaved' [src]="employee.profile_image_url" height="50" width="50"> 
</from> 

Typoskript:

save(fileName:string): void { 
    if (this.employee.bank_details == null) { 
     this.employeeService.add_bankDetails(this.model, this.employee); 
    } 
    if (this.employee.current_address == null) { 
     this.employeeService.add_addressDetails(this.model, this.employee); 
    } 
    if (this.employee.permanent_address == null) { 
     this.employeeService.add_PaddressDetails(this.modelP, this.employee); 
    } 
    this.employee.full_name = (`${this.employee.first_name} ${this.employee.last_name}`); 
    this.employeeService.update(this.employee); 
    //here you call file changing function 
    fileChange(fileName); 
} 


fileChange(event: any){ 
    .... 
    //add this at the end 
    isSaved=true; 
} 
+0

es funktioniert nicht – Krunal

+0

Haben Sie eine Fehlermeldung oder es tut nichts? – Vega

+0

kein Fehler msg .. es funktioniert ordnungsgemäß, aber es ist getan, bevor Formular – Krunal