控制器"如何使用&QUOT调用$ $范围适用于()。句法 [英] How to call $scope.$apply() using "controller as" syntax
问题描述
我想我用我的控制器$范围限制尽可能与控制器为语法替换它。
I am trying to limit my use of "$scope" in my controllers as much as possible and replace it with the "Controller as" syntax.
我现在的问题是,我不知道如何调用$范围。$适用()在我的控制器不使用$范围。
My current problem is that i'm not sure how to call $scope.$apply() in my controller without using $scope.
编辑:我使用的打字稿1.4与角度结合
I am using TypeScript 1.4 in conjunction with 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();
});
和它的(阵列词表和notUsedWordList)
是不是在我看来被更新:
And it's (the arrays wordList and notUsedWordList) is not being updated in my view:
<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>
随着应用的$同样的思路,有没有调用$范围的另一种方式。$使用控制器?
Along the same lines of $apply, is there another way of calling $scope.$on using "Controller as"?
谢谢!
推荐答案
要在这里回答手头上的问题,你可以使用 $范围()
方法的控制器时,使用控制器的语法,只要你通过 $范围
作为参数传递给函数。然而,使用的主要好处之一控制器为语法不使用 $范围
,这就造成了进退两难的境地。
To answer the question at hand here, you can use $scope()
methods in a controller when using the controller-as syntax, as long as you pass $scope
as a parameter to the function. However, one of the main benefits of using the controller-as syntax is not using $scope
, which creates a quandary.
正如在评论中讨论的,一个新的问题将由海报制定审查具体code要求 $范围
首先,一些对于重组,如果可能的话。
As was discussed in the comments, a new question will be formulated by the poster to review the specific code requiring $scope
in the first place, with some recommendations for re-structuring if possible.
这篇关于控制器&QUOT;如何使用&QUOT调用$ $范围适用于()。句法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!