angularjs – 如何调用$scope $apply()使用“controller as”语法

前端之家收集整理的这篇文章主要介绍了angularjs – 如何调用$scope $apply()使用“controller as”语法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图尽可能地限制我在控制器中使用$ scope,并将其替换为Controller作为语法。

我当前的问题是,我不知道如何调用$ scope。$ apply()在我的控制器中,而不使用$ scope。

编辑:我正在使用TypeScript 1.4与角度

我有这个功能

  1. setWordLists() {
  2. this.fetchInProgress = true;
  3. var campaignId = this.campaignFactory.currentId();
  4. var videoId = this.videoFactory.currentId();
  5.  
  6. if (!campaignId || !videoId) {
  7. return;
  8. }
  9.  
  10. this.wordsToTrackFactory.doGetWordsToTrackModel(campaignId,videoId)
  11. .then((response) => {
  12. this.fetchInProgress = false;
  13. this.wordList = (response) ? response.data.WordList : [];
  14. this.notUsedWordList = (response) ? response.data.NotUsedWords : [];
  15. });
  16. }

被叫来

  1. $scope.$on("video-switch",() => {
  2. this.setWordLists();
  3. });

它是(数组wordList而notUsedWordList)
在我看来没有更新:

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

按照同样的条件申请,是否有另一种方式调用$ scope。$ on使用Controller作为?

谢谢!

@H_301_25@ 为了回答这里的问题,只要将$ scope作为参数传递给函数,就可以在控制器中使用$ scope()方法来使用controller-as语法。然而,使用控制器作为语法的主要好处之一是不使用$ scope,这会造成一个困难。

正如评论中讨论的那样,海报将会提出一个新问题,以便首先审查需要范围的具体代码,并提出一些可能的重组建议。

猜你在找的Angularjs相关文章