我已经开始学习了Angular2
但我想使用提交表格http.post()
到我的 Web API,但我不能。
在您的组件中,您只需在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');
});
}
});
您需要添加HTTP_PROVIDERS
引导您的应用程序时:
document.addEventListener('DOMContentLoaded', function() {
ng.platform.browser.bootstrap(Cmp, [ ng.http.HTTP_PROVIDERS]);
});
这是相应的 plunker:https://plnkr.co/edit/Fl2pbKxBSWFOakgIFKaf?p=preview https://plnkr.co/edit/Fl2pbKxBSWFOakgIFKaf?p=preview.
希望对你有帮助,
蒂埃里
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)