出于某些奇怪的原因,只是在线上没有教程或代码示例,显示了如何使用Angular2反应性形式,而不是简单输入或选择下拉下拉。
我需要创建一个表格,以便用户选择其化身。(图像文件(
以下内容不起作用。(即,阿凡达属性从未显示任何价值更改。(
profile.component.html:
<form [formGroup]="profileForm" novalidate>
<div class="row">
<div class="col-md-4 ">
<img src="{{imgUrl}}uploads/avatars/{{getUserAvatar}}" style="width:150px; height:150px;float:left;border-radius:50%;margin-right:25px;margin-left:10px;">
<div class="form-group">
<label>Update Profile Image</label>
<input class="form-control" type="file" formControlName="avatar">
</div>
</div>
<div class="col-md-8 ">
<div class="form-group">
<label >Firstname:
<input class="form-control" formControlName="firstname">
</label>
</div>
<div class="form-group">
<label >Lastname:
<input class="form-control" formControlName="lastname">
</label>
</div>
<div class="form-group">
<label >Email:
<input class="form-control" formControlName="email">
</label>
</div>
<div class="form-group">
<label >Password:
<input class="form-control" type="password" formControlName="password">
</label>
</div>
</div>
</div>
</form>
<p>Form value: {{ profileForm.value | json }}</p>
<p>Form status: {{ profileForm.status | json }}</p>
profile.component.ts:
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import {Config} from '../../services/config.service';
import {AuthService} from '../../services/auth.service';
import {User} from '../../models/user.model';
@Component({
selector: 'app-profile',
templateUrl: './profile.component.html',
styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {
authUser:User;
profileForm : FormGroup;
constructor(private authService:AuthService, private fb: FormBuilder) {}
createForm() {
this.profileForm = this.fb.group({
firstname: [this.authUser.firstname, Validators.required ],
lastname: [this.authUser.lastname, Validators.required ],
email: [this.authUser.email, Validators.required ],
avatar: [this.authUser.avatar, Validators.required ],
password:['xxxxxx', Validators.minLength(4)]
});
}
ngOnInit() {
this.authUser = this.authService.getAuthUser();
this.createForm();
}
可以在此处找到简单的答案。https://devblog.dymel.pl/2016/09/02/upload-file-image-image-image-image-angular2-aspnetcore/
html
<input #fileInput type="file"/>
<button (click)="addFile()">Add</button>
component.ts
@ViewChild("fileInput") fileInput;
addFile(): void {
let fi = this.fileInput.nativeElement;
if (fi.files && fi.files[0]) {
let fileToUpload = fi.files[0];
this.uploadService
.upload(fileToUpload)
.subscribe(res => {
console.log(res);
});
}
}
service.ts
upload(fileToUpload: any) {
let input = new FormData();
input.append("file", fileToUpload);
return this.http.post("/api/uploadFile", input);
}
我有点晚了,但是对于可能来这里寻找相同解决方案的任何人 - 这是我的文件输入访问器,可与反应性或模板驱动的表单一起使用。演示在这里。
提供了一些可选验证,可用于检查图像尺寸和文件大小,扩展,类型,默认情况下是禁用的。
npm i file-input-accessor
并将模块添加到您的appModule导入:
import {BrowserModule} from '@angular/platform-browser';
import {FileInputAccessorModule} from "file-input-accessor";
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FileInputAccessorModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
然后像其他任何输入一样使用您的文件输入:
<!--Reactive Forms-->
<input type="file" multiple [formControl]="someFileControl" />
<input type="file" multiple formControlName="someFileControl" />
<!--Template-driven-->
<input type="file" name="file-input" multiple [(ngModel)]="fileList" />
您可以像其他任何反应性控制一样订阅Valuechanges属性。
您可以使用以下方法以任何类型的形式上传图像。
将一种更改方法暴露于您的控件。
<input class="form-control" type="file" name="avatar" (change)="imageUpload($event)">
<img [src]="imageUrl" />
在您的班级中添加下面的逻辑。
// Declare the variable.
imageUrl: any;
//method definition in your class
imageUpload(e) {
let reader = new FileReader();
//get the selected file from event
let file = e.target.files[0];
reader.onloadend = () => {
//Assign the result to variable for setting the src of image element
this.imageUrl = reader.result;
}
reader.readAsDataURL(file);
}
}
上传图像后,您可以使用 this.imageurl 更新您的表单模型。要将图像或文件上传到服务器,您可以从下面的链接中获取参考。
如何在Angular2
中上传文件让我知道该解决方案是否适合您。