8

I started working with AngularJS and I embraced the convention for writing controllers with this, not with $scope. So my controllers look like this:

myApp.controller("SomeController", function(){
    this.myModel={id:-1, name:"empty"};
});

<div ng-controller="SomeController as ctrl">
    <input type="text" ng-model="ctrl.myModel.name" />
</div>

Now, I changed the myModel object in the controller in a way like this:

this.myModel=someOtherObjectFromAForeignSource;

... and the value inside the input control doesn't change. I read about the $apply function and it's use but since i use the "this" convention, I don't have a $scope variable.

How do I call the $apply method?

7
  • Why do you think you need $apply()? Where does that other data come from? - Your input is also set to the name property - does your new object contain that property? Commented Dec 19, 2014 at 14:35
  • Yes, it contains the name property - this new object is coming from a 3rd party control, and angular can't register the change automatically. Anyway, my question is still how to use the $apply method without the use of $scope? Commented Dec 19, 2014 at 14:40
  • I think, you can't use $apply without $scope or $rootScope. Commented Dec 19, 2014 at 14:45
  • Can you post the code where you do this.myModel=someOtherObjectFromAForeignSource;. Because the model should be updated IF you are not using a third party library in your controller (like jQuery). Commented Dec 19, 2014 at 14:47
  • There is apply() method in javascript. consider using that.. Commented Dec 19, 2014 at 14:48

1 Answer 1

17

You can certainly still use $scope with the controller as syntax with no issue.

In fact that is how you would handle events ($on, $broadcast, $emit) just inject it into your controller:

app.controller('MyCtrl', function($scope){
   //set your properties/methods as you are
   this.message = 'foo';
   this.yell = function(){
     this.message = this.message.toUpperCase() + '!!!';
   };

  var vm = this;
  //and use $apply as needed
  somethingOutsideOfAngular(function(value){
    $scope.$apply(function(){
      vm.message = value;
    });
  });
});
Sign up to request clarification or add additional context in comments.

6 Comments

So I still need to inject $scope if I want to use $apply? I tried it like this and it certainly works but I thought there was another way.
Meteo, that is correct, because $apply is a method on the $scope object.
It isn't exactly the answer I wanted but OK - do you perhaps know where could I confirm that you really NEED to inject $scope for $apply?
The documentation for $apply which states that it is a method upon the Scope object is here docs.angularjs.org/api/ng/type/$rootScope.Scope#$apply And since based upon your need to call $apply the only way to get to it is the actual Scope object by injecting $scope
This is certainly a limitatiom of using controllerAs syntax, because you will have to inject and invoke $scope anyway as shown in the answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.