AngularJS - 在广播事件后更新指令的视图

AngularJS - updating directive's view after broadcasting event

本文关键字:更新 指令 视图 事件 广播 AngularJS      更新时间:2023-09-26

在我的角度应用程序(使用 ES6)中,我需要在从其他指令触发一些自定义事件后进行 ajax 调用,代码如下:

class GridController {
constructor($scope, $rootScope, MyService) {
    this.$scope = $scope;
    this.$rootScope = $rootScope;
    this.MyService = MyService
    this.MyService.getItems(arg1, arg2).then((resp) => {
                this.viewModel= resp;
            });
    this.$rootScope.$on('myEvent', (e, args) => {
        this.MyService.getItems(arg1, args[1]).then((resp) => {
                this.viewModel= resp;
            });
        }
    });
}

问题是视图在成功的 ajax 调用后没有更新。有什么想法为什么会发生这种情况吗?

可能是模型更新问题,请尝试此操作。

$scope.$apply();

也许 ajax-call 发生在 angulars 消化周期之外。您试过打电话给$scope.$digest()?这将触发当前范围的观察程序;他们应该接受模型更改。