Angular2 Meteor Bind Data from calling this.call

2019-07-26 05:04发布

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'.

2条回答
欢心
2楼-- · 2019-07-26 05:50

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

查看更多
时光不老,我们不散
3楼-- · 2019-07-26 05:51

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;
    });
  });
}
查看更多
登录 后发表回答