Angular2 Meteor Bind Data from calling this.call

这一生的挚爱 提交于 2019-12-11 13:17:07

问题


Methods

Meteor.methods({
    'test' : function(test: string) {
        return test;
    }
})

Component

My Class extends MeteorComponent

show: string;
constructor() {
     this.call('test', 'txt', (err, res) => {
          this.show = res
     });
}

view

<span>{{show}}</span>

it shows nothing, as I expect it will show 'txt'.


回答1:


Unlike autorun, call has no parameter to tell it to run inside the NgZone, so Angular's change-detection won't kick in.

You'll need to write it this way:

constructor(zone: NgZone) {
  this.call('test', 'txt', (err, res) => {
    zone.run(() => {
      this.show = res;
    });
  });
}



回答2:


Just add an explanation for @Martin C.'s answer.

In Angular2-Meteor 0.5.6 (not published to NPM yet), you should be able to use autoBind.

  this.call('test', 'txt', (err, res) => {
    this.show = res;
  }, true);  // set `autoBind` to `true` here

https://github.com/Urigo/angular2-meteor/issues/279



来源:https://stackoverflow.com/questions/37849791/angular2-meteor-bind-data-from-calling-this-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!