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

【字号: 日期:2024-04-11浏览:41作者:雯心
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解决如何使用“ controller as”语法调用$ scope$ apply()?

为了回答此处的问题$scope(),只要$scope将参数作为参数传递给函数,就可以在使用controller-as语法时在控制器中使用方法。但是,使用controller-as语法的主要好处之一是没有使用$scope,这会产生一个难题。

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

解决方法

我试图限制我$scope在控制器中的使用,并用Controller as语法替换它。

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

编辑:我使用TypeScript 1.4结合角度

我有这个功能

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();});

在我看来,(数组wordList和notUsedWordList)没有更新:

<div ng-repeat='words in wordTrack.wordList'> <div ng-class='(words.IsPositive)? ’posWordWell’: ’negWordWell’ '> <strong class='wordListWord'>{{words.Word}}</strong> <div class='wordListIcon'> <div ng-class='(words.IsPositive)? ’happyWhiteIcon’: ’sadWhiteIcon’ '></div> </div> </div> <div class='col-md-2'> <span aria-hidden='true' ng-click='wordTrack.removeWord(words.Word)'></span> </div></div>

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

谢谢!

相关文章: