如何使用 "controller as" 语法调用 $scope.$apply()

How to call $scope.$apply() using "controller as" syntax

我正在尝试尽可能限制我在控制器中使用 $scope 并将其替换为 Controller as 语法。

我目前的问题是我不确定如何在不使用 $scope 的情况下在我的控制器中调用 $scope.$apply()

编辑:我将 TypeScript 1.4 与 angular

结合使用

我有这个功能

setWordLists() {
  this.fetchInProgress = true;
  var campaignId = this.campaignFactory.currentId();
  var videoId = this.videoFactory.currentId();

  if (!campaignId || !videoId) {
    return;
  }

  this.wordsToTrackFactory.doGetWordsToTrackModel(campaignId, videoId)
  .then((response) => {
    this.fetchInProgress = false;
    this.wordList = (response) ? response.data.WordList : [];
    this.notUsedWordList = (response) ? response.data.NotUsedWords : [];
  });
}

正在从

呼叫
$scope.$on("video-switch",() => {
           this.setWordLists();
});

它是(数组 wordListnotUsedWordList) 在我看来没有更新:

<div class="wordListWellWrapper row" ng-repeat="words in wordTrack.wordList">
  <div class="col-md-5 wordListWell form-control" ng-class="(words.IsPositive)? 'posWordWell': 'negWordWell' ">
    <strong class="wordListWord">{{words.Word}}</strong>
    <div class="wordListIcon">
      <div class="whiteFaceIcon" ng-class="(words.IsPositive)? 'happyWhiteIcon': 'sadWhiteIcon' "></div>
    </div>
  </div>
  <div class="col-md-2">
    <span aria-hidden="true" class="glyphicon-remove glyphicon" ng-click="wordTrack.removeWord(words.Word)"></span>
  </div>
</div>

按照 $apply 的相同思路,是否有另一种使用 Controller as 调用 $scope.$on 的方法?

谢谢!

这里要回答手头的问题,在使用controller-as 语法时,可以在控制器中使用$scope() 方法,只要将$scope 作为参数传递给函数即可。但是,使用 controller-as 语法的主要好处之一是不使用 $scope,这会造成一个难题。

正如评论中所讨论的那样,发布者将制定一个新问题,首先审查需要 $scope 的特定代码,并在可能的情况下提出一些重组建议。