Naz*_*kib 9 javascript angular2-http angular
我已经开始学习,Angular2但我想使用http.post()我的Web API 提交表单,但我不能.
Thi*_*ier 11
在组件中,您只需要在submit事件上附加侦听器并利用该http对象来执行HTTP请求.此对象先前已注入组件的构造函数中.
var Cmp = ng.core.
Component({
selector: 'cmp'
template: `
<form (submit)="submitForm()">
<input [(ngModel)]="element.name"/>
<button type="submit">Submit the form</button>
</form>
`
}).
Class({
constructor: [ ng.http.Http, function(http) {
this.http = http;
}],
submitForm: function() {
var headers = new ng.http.Headers();
headers.append('Content-Type', 'application/json');
this.http.post('http://...', JSON.stringify(this.element), {
headers: headers
}).subscribe(function(data) {
console.log('received response');
});
}
});
Run Code Online (Sandbox Code Playgroud)
您需要HTTP_PROVIDERS在引导应用程序时添加:
document.addEventListener('DOMContentLoaded', function() {
ng.platform.browser.bootstrap(Cmp, [ ng.http.HTTP_PROVIDERS]);
});
Run Code Online (Sandbox Code Playgroud)
这是相应的plunkr:https://plnkr.co/edit/Fl2pbKxBSWFOakgIFKaf ? p = preview .
希望它对你有帮助,蒂埃里
| 归档时间: |
|
| 查看次数: |
19367 次 |
| 最近记录: |