Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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!

like image 562
Sherman Szeto Avatar asked Jan 18 '15 17:01

Sherman Szeto


People also ask

What is scope apply ()?

$scope. $apply() takes a function or an Angular expression string, and executes it, then calls $scope. $digest() to update any bindings or watchers.

How to define a controller in AngularJS?

In AngularJS, a controller is defined by a Javascript construction function, which is used in AngularJS scope and also the function $scope) is defined when the controller is defining and it returns the concatenation of the $scope. firstname and $scope. lastname.

What is $scope in AngularJS?

The Scope in AngularJS is the binding part between HTML (view) and JavaScript (controller) and it is a built-in object. It contains application data and objects. It is available for both the view and the controller. It is an object with available properties and methods. There are two types of scopes in Angular JS.

What is attrs in AngularJS?

scope is an AngularJS scope object. element is the jqLite-wrapped element that this directive matches. attrs is a hash object with key-value pairs of normalized attribute names and their corresponding attribute values.


1 Answers

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.

like image 143
Claies Avatar answered Oct 21 '22 20:10

Claies