Angular2:如何在ES5中使用Http?
Angular 2: How to use Http in ES5?
目前,我正在研究 Angular 2. 我尝试在 ES5 中使用 Http,但无法正常工作。错误说,"Http is not defined".
这是我的代码:
function Service() {}
Service.prototype.greeting = function() {
return 'Hello';
};
var Cmp = ng.
Component({
selector: 'cmp',
providers: [Service],
directives: [ng.NgFor],
injectors: [ng.Http, ng.HTTP_PROVIDERS],
templateUrl: 'hello.html'
}).
Class({
constructor: [Service, function Cmp(service) {
this.greeting = service.greeting();
ng.Http.get('people.json');
}],
});
document.addEventListener('DOMContentLoaded', function() {
ng.bootstrap(Cmp);
});
谁能帮我解决这个问题?谢谢。
终于解决了自己的问题
这是我的代码:
function Service() {}
Service.prototype.greeting = function() {
return 'Hello';
};
var Cmp = ng.
Component({
selector: 'cmp',
providers: [Service, ngHttp.HTTP_PROVIDERS],
template: '{{greeting}}, {{result.name}}!'
}).
Class({
constructor: [Service, ngHttp.Http, function Cmp(service, Http) {
this.greeting = service.greeting();
this.result = {};
this.http = Http;
this.mapPeople().subscribe(function(result){
this.result = result;
}.bind(this));
}],
mapPeople: function(){
return this.http.get('people.json').map(function (res) {
return res.json();
});
}
});
document.addEventListener('DOMContentLoaded', function() {
ng.bootstrap(Cmp);
});
原来,Http对象在ngHttp中。我认为由于 Angular 2 目前仍处于 Alpha 版本并且变化迅速,因此缺少文档。我希望这能帮助那些和我有同样问题的人。
为了让它与测试版一起使用,我必须将 ngHttp 更改为 ng.http
对于Angular 2 Beta 版本,它的工作方式如下
(function(app) {
app.MyName = ng.core
.Component({
selector: 'my-name',
template: '<span>Dave</span>',
providers: [ng.http.HTTP_PROVIDERS]
})
.Class({
constructor: [ng.http.Http, function(http) {
}]
});
app.HelloApp =
ng.core.Component({
selector: 'hello-app',
template: '<h1>Hello Angular 2!</h1><my-name></my-name>',
directives: [app.MyName]
})
.Class({
constructor: function(){}
});
})(window.app || (window.app = {}));
目前,我正在研究 Angular 2. 我尝试在 ES5 中使用 Http,但无法正常工作。错误说,"Http is not defined".
这是我的代码:
function Service() {}
Service.prototype.greeting = function() {
return 'Hello';
};
var Cmp = ng.
Component({
selector: 'cmp',
providers: [Service],
directives: [ng.NgFor],
injectors: [ng.Http, ng.HTTP_PROVIDERS],
templateUrl: 'hello.html'
}).
Class({
constructor: [Service, function Cmp(service) {
this.greeting = service.greeting();
ng.Http.get('people.json');
}],
});
document.addEventListener('DOMContentLoaded', function() {
ng.bootstrap(Cmp);
});
谁能帮我解决这个问题?谢谢。
终于解决了自己的问题
这是我的代码:
function Service() {}
Service.prototype.greeting = function() {
return 'Hello';
};
var Cmp = ng.
Component({
selector: 'cmp',
providers: [Service, ngHttp.HTTP_PROVIDERS],
template: '{{greeting}}, {{result.name}}!'
}).
Class({
constructor: [Service, ngHttp.Http, function Cmp(service, Http) {
this.greeting = service.greeting();
this.result = {};
this.http = Http;
this.mapPeople().subscribe(function(result){
this.result = result;
}.bind(this));
}],
mapPeople: function(){
return this.http.get('people.json').map(function (res) {
return res.json();
});
}
});
document.addEventListener('DOMContentLoaded', function() {
ng.bootstrap(Cmp);
});
原来,Http对象在ngHttp中。我认为由于 Angular 2 目前仍处于 Alpha 版本并且变化迅速,因此缺少文档。我希望这能帮助那些和我有同样问题的人。
为了让它与测试版一起使用,我必须将 ngHttp 更改为 ng.http
对于Angular 2 Beta 版本,它的工作方式如下
(function(app) {
app.MyName = ng.core
.Component({
selector: 'my-name',
template: '<span>Dave</span>',
providers: [ng.http.HTTP_PROVIDERS]
})
.Class({
constructor: [ng.http.Http, function(http) {
}]
});
app.HelloApp =
ng.core.Component({
selector: 'hello-app',
template: '<h1>Hello Angular 2!</h1><my-name></my-name>',
directives: [app.MyName]
})
.Class({
constructor: function(){}
});
})(window.app || (window.app = {}));