現在、Angular 2では、ユーザーがプロファイル(電子メールとパスワード)を更新できるフォームを開発中です。角度2 - 入力ファイル付きのモデル駆動型(ファイルアップロード)
ユーザーedit.component.html:
<form novalidate="novalidate" [ngFormModel]="form" (ngSubmit)="form.valid && onSubmit()">
<fieldset>
<div>
<label for="email">
Email
</label>
<input id="email" type="email" #email="ngForm" ngControl="email"/>
<div class="error" *ngIf="email.control.hasError('required') && email.touched">
This value is required.
</div>
</div>
<div>
<label for="password">
Password
</label>
<input id="password" type="password" #password="ngForm" ngControl="password"/>
<div class="error" *ngIf="password.control.hasError('required') && password.touched">
This value is required.
</div>
</div>
</fieldset>
<button type="submit" [disabled]="!form.valid">
Save
</button>
</form>
ユーザーedit.component.ts:
@Component({
selector: 'user-edit',
templateUrl: 'app/components/user-edit/user-edit.component.html',
})
export class UserEditComponent implements OnInit {
form: any;
errors = [];
constructor(
private _formBuilder: FormBuilder,
private _router: Router,
private _userService: UserService
) {}
ngOnInit() {
this.form = this._formBuilder.group({
'email': ['', Validators.required)],
'password': ['', Validators.required],
});
}
onSubmit() {
var self = this;
this._userService.edit(this.form.value, function (response: Response) {
console.log('OK');
}, function (response: Response) {
console.log('ERROR');
});
}
}
user.service.ts:
@Injectable()
export class UserService {
constructor (
private _http: Http
) {}
edit(user: User, cfOnNext?: Function, cfOnError?: Function): void {
let body = JSON.stringify(user);
let headers = new Headers({
'Content-Type': 'application/json',
});
let options = new RequestOptions({
headers: headers,
});
self._http.put('https://api.dom/me', body, options).subscribe(function (response) {
if (cfOnNext) {
cfOnNext(response);
}
}, function (response) {
if (cfOnError) {
cfOnError(response);
}
});
}
}
今日私が欲しいですファイルタイプフィールドを追加するだけで写真をアップロードできます。
しかし、私はその件に関する情報がほとんど見つかりませんでした。
唯一の解決策は、私が何をしたいのか近づいているようだがここにある: https://www.thepolyglotdeveloper.com/2016/02/upload-files-to-node-js-using-angular-2/
は、これを達成するためのより良い解決策はありますか?最大限の標準化が必要なため、私のコードに近いソリューションですか?例えば、私がすでに使用している$ http Angularサービスを使用していますか?
ありがとうございます!
回答が[これに正確に似て答えました答え](http://stackoverflow.com/a/35985489/2435473)(decto各単語はコピーされたままコピーされます)、なぜあなたはそれをそれを再度書き直すのではなく、複製してください。 –
ええ、私は質問がその質問とは異なるので、回答として投稿すると思います。 –
それであなたがそう思っていたり、答えのリンクをコメントに追加したりすると、重複としてマークしないでください。 –