如何使用angular更新jQuery代码中的数据绑定?

时间:2019-04-11 06:18:25

标签: jquery angular

我在我的角度项目中使用emojiarea jQuery插件。一切正常,但我想将emojiarea文本更新为有角度的公共变量。我该怎么办?我尝试使用zone.js,但没有成功。

ngAfterViewInit(): void {
    $('#example').emojioneArea({
      autoHideFilters: true,
      saveEmojisAs: 'unicode',
      events: {
        keyup: function (editor, e) {
          this.message = this.getText();
          this.zone.run(() => this.onPlayerStateChange(e));
          this.chRef.detectChanges();
          console.log(this.message);
        }
      }
    });
 }

我想在Submit函数中获取this.message变量的值。

2 个答案:

答案 0 :(得分:0)

您可以这样使用

ngAfterViewInit(): void {
    let that = this;
    $('#example').emojioneArea({
      autoHideFilters: true,
      saveEmojisAs: 'unicode',
      events: {
        keyup: function (editor, e) {
          that.message = that.getText();
          that.zone.run(() => that.onPlayerStateChange(e));
          that.chRef.detectChanges();
          console.log(that.message);
        }
      }
    });
 }

答案 1 :(得分:0)

我发现了this.getText();从emojioneArea返回当前文本,并且this.message是angulars变量,因此您应该保存对angular组件的引用,以便它保存此消息变量,请尝试编写此

 let that = this;
    $('#example').emojioneArea({
      autoHideFilters: true,
      saveEmojisAs: 'unicode',
      events: {
        keyup: function (editor, e) {
          that.message = this.getText();
          that.zone.run(() => that.onPlayerStateChange(e));
          that.chRef.detectChanges();
          console.log(that.message);
        }
      }
    });