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

She*_*eto 18 angularjs angularjs-scope

我试图尽可能地限制我$scope在控制器中的使用,并用Controller as语法替换它.

我目前的问题是,我不知道如何$scope.$apply()不使用我的控制器$scope.

编辑:我正在使用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 : [];
  });
}
Run Code Online (Sandbox Code Playgroud)

被叫来

$scope.$on("video-switch",() => {
           this.setWordLists();
});
Run Code Online (Sandbox Code Playgroud)

它的(数组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>
Run Code Online (Sandbox Code Playgroud)

沿着相同的路线$apply,有没有调用的另一种方法$scope.$on使用Controller as

谢谢!

Cla*_*ies 6

要回答这里的问题$scope(),只要$scope将参数作为参数传递给函数,就可以在使用控制器作为语法时使用控制器中的方法.但是,使用控制器作为语法的主要好处之一是不使用$scope,这会产生一个窘境.

正如评论中所讨论的那样,海报将制定一个新问题,以便首先审查所需的具体代码$scope,并在可能的情况下提出一些重新构建的建议.