【问题标题】:How to manually run digest in an Angular component如何在 Angular 组件中手动运行摘要
【发布时间】:2017-02-04 02:48:18
【问题描述】:

我正在尝试编写这样的控制器:

export default class someServiceCtrl{

    constructor(someService) {
        this.someService= someService;
        this.$isEditable = false;
        this.options = {};
        this.items=[];
        this.emptyData = true;
    }

    switchIsEditable(){
        this.$isEditable = !this.$isEditable;
        if(this.$isEditable){
            angular.element(document).on('keydown', function (e){
                if(e.keyCode === 27 || e.keyCode === 31){
                    this.$isEditable = false;
                }
            }.bind(this));
        }
    }

    dialogGotBlurred(){
        this.$isEditable = false;
    }
}

someServiceCtrl.$inject = ['someService'];

但是 $isEditable 永远不会改变。 我被困在这一点上。

如何让它发挥作用?

谢谢。

【问题讨论】:

    标签: javascript angularjs ecmascript-6 components


    【解决方案1】:

    我能够让它工作的方法是注入$rootScope 并调用$rootScope.$digest()。不是最优雅的解决方案,但它应该可以让你做你想做的事。

    【讨论】:

      猜你喜欢
      • 2018-05-30
      • 1970-01-01
      • 2020-05-23
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      • 2016-04-05
      • 2019-09-05
      • 2016-10-03
      相关资源
      最近更新 更多