Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在es5中使用angular 2 http客户端_Javascript_Angular - Fatal编程技术网

Javascript 在es5中使用angular 2 http客户端

Javascript 在es5中使用angular 2 http客户端,javascript,angular,Javascript,Angular,我们正在尝试将angular 1应用程序逐模块升级为angular 2;由于我们现有的项目是基于es5的,所以我们决定将其用于angular 2。到目前为止,我们已经在某种程度上找到了upgradeAdapter和DI,但似乎在使用http客户端时遇到了障碍。代码如下: HTML 所有与ng2相关的js文件都被连接到ng2.js中 ng2.js app = giddh.webApp; class diTest { constructor (){ } get(){ ret

我们正在尝试将angular 1应用程序逐模块升级为angular 2;由于我们现有的项目是基于es5的,所以我们决定将其用于angular 2。到目前为止,我们已经在某种程度上找到了upgradeAdapter和DI,但似乎在使用http客户端时遇到了障碍。代码如下:

HTML

所有与ng2相关的js文件都被连接到ng2.js中

ng2.js

app = giddh.webApp;

class diTest {
  constructor (){
  }

  get(){
    return 'injected from diTest';
  }
};

console.log(ng.http)

app.AppComponent =
  ng.core.Component({
    selector: 'my-app',
    providers: [diTest, ng.http.HTTP_PROVIDERS],
    templateUrl: '/public/webapp/ng2/audit_logs/audit-logs.html'
  })
  .Class({
    constructor:[diTest,ng.http.HTTP_PROVIDERS, function(di, http) {
      this.test = di.get();
      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();
      });
    }
});

})()



var upgradeAdapter = new ng.upgrade.UpgradeAdapter();

angular.element(document.body).ready(function() {
  upgradeAdapter.bootstrap(document.body, ['giddhWebApp']);
});

// downgrade ng2 components to ng1 directives
app.directive('myApp',     upgradeAdapter.downgradeNg2Component(app.AppComponent));
我在控制台中得到的错误是:

实例化错误{u wrapperMessage:“DI异常”,_originalException:TypeError:this.http.get不是函数 在app.AppComponent.ng.core.Component.Class.mapPeople…,_originalStack:“类型错误:this.http.get不是函数↵ 在…0[作为viewFactory](viewFactory_Hostclass0:73:25)”,_context:null,_wrapperStack:“错误:DI异常”↵ 在实例化error.Wrap…ngular2/bundles/angular2 all.umd.dev.js:16347:37“…}”


提前感谢:)

您需要在构造函数级别指定
Http\u提供程序的
Http
实例,因为它只对应于您希望注入构造函数中的对象的类型:

.Class({
  constructor:[diTest,ng.http.Http, function(di, http) { // <---------
    this.test = di.get();
    this.result = {};
    this.http = http;
    this.mapPeople().subscribe(function(result){
      this.result = result;
    }.bind(this));
  }],
请参阅以下链接:

ng.core.Component({
  selector: 'my-app',
  providers: [diTest, ng.http.HTTP_PROVIDERS],
  templateUrl: '/public/webapp/ng2/audit_logs/audit-logs.html'
})