0

Here I have :

var app = angular.module('app');
app.controller("myController", function () {
        var vm = this;
        vm.myFunction = function() { alert('foo'); };
});

app.animation('.animate', ["$timeout", function($timeout) {
      var vm = this;
      return {
        addClass: function(element, className, doneFn) {
            $timeout(function() {
                console.log('this is displayed');
                vm.myFunction(); // Doesn't work !
            });
        }
      }
}]);

When I add a class in the template, addClass gets fired. However, vm.myFunction() doesn't, because it does not exist.

How do we do this in angular ?

2
  • what kind of syntax for a controller this is... where is the controller name? Commented Mar 2, 2016 at 6:57
  • Sorry I just forgot to put in in the sample ! Commented Mar 2, 2016 at 7:03

2 Answers 2

0

Some different from yours but I thought this can help you...

in HTML

<div id="outer" ng-controller="myController"></div>

in JS

var app = angular.module('app');
app.controller('myController', function ($scope) {

 $scope.myFunction = function() { alert('foo'); };

});

var scope = angular.element($("#outer")).scope();
scope.myFunction();
Sign up to request clarification or add additional context in comments.

2 Comments

I actually found the same answer here : stackoverflow.com/questions/23887872/… and that did the trick ! Thanks !
Although getting the scope by hack, will do, but this is not the best practice that one should followed. If one needs to call the function, then why shouldn't one make use of angular in pure. There are many in it like -> providers, services, factory, etc. which does the job. In your case, it is recommend to make use of either of these.
0

Modify your code as the following:

var app = angular.module('app');
app.controller('myController', function ($scope) {
        var vm = this;
        vm.myFunction = function() { alert('foo'); };
        $scope = vm;
});

app.animation('.animate', ["$timeout", 'myController', function($timeout, myController) {
      var vm = myController;
      return {
        addClass: function(element, className, doneFn) {
            $timeout(function() {
                console.log('this is displayed');
                vm.myFunction(); // Doesn't work !
            });
        }
      }
}]);

3 Comments

Thanks for answering ! I did your changes, but I got the following error : "Error: [$injector:unpr] Unknown provider: myController <- myController <- .animate-animation"
Maybe this is because "Attempting to inject one controller into another will also throw an Unknown provider error" from docs.angularjs.org/error/$injector/unpr
Since you haven't shared the demo of your code, it was impossible for me to check and verify the same. You can check in my answers, I most of the time support my code with a working demo. it might have created issue due to some missing module or error.

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.