本文介绍了角2:如何在ES5使用HTTP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我玩弄角2.我尝试使用HTTP ES5,但不能让它的工作。错误说,HTTP是没有定义。

Currently, I'm playing around with Angular 2. I try to use Http in ES5 but can't make it working. The error says, "Http is not defined".

下面是我的code:

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);
});

任何人可以帮助我解决呢?谢谢你。

Can anybody help me on solving this? Thank you.

推荐答案

我终于成功地解决我自己的问题。

I finally managed to solve my own problem.

下面是我的code:

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。我想既然角2,目前仍处于Alpha版和迅速变化,该文档缺乏。我希望这将有助于人在那里,因为我有谁有同样的问题。

Turn out, the Http object is in ngHttp. I think since Angular 2 is currently still in Alpha version and rapidly changing, the documentation is lacking. I hope this would help people out there who have the same problem as I had.

这篇关于角2:如何在ES5使用HTTP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 03:45