如何使用“controller as"调用 $scope.$apply()句法 [英] How to call $scope.$apply() using "controller as" syntax

查看:20
本文介绍了如何使用“controller as"调用 $scope.$apply()句法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我将 TypeScript 1.4 与 angular 结合使用

我有这个功能

setWordLists() {this.fetchInProgress = true;var campaignId = this.campaignFactory.currentId();var videoId = this.videoFactory.currentId();如果 (!campaignId || !videoId) {返回;}this.wordsToTrackFactory.doGetWordsToTrackModel(campaignId, videoId).then((响应) => {this.fetchInProgress = false;this.wordList = (响应) ?response.data.WordList : [];this.notUsedWordList = (响应) ?response.data.NotUsedWords : [];});}

被调用

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

它是(数组wordListnotUsedWordList)我认为没有更新:

<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 class="col-md-2"><span aria-hidden="true" class="glyphicon-remove glyphicon" ng-click="wordTrack.removeWord(words.Word)"></span>

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

谢谢!

解决方案

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

正如评论中所讨论的,发帖者将首先提出一个新问题来审查需要 $scope 的特定代码,并在可能的情况下提供一些重构建议.

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.

Edit: I am using TypeScript 1.4 in conjunction with angular

I have this function

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 : [];
  });
}

being called from

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

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?

Thanks!

解决方案

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.

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.

这篇关于如何使用“controller as"调用 $scope.$apply()句法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆