1

How can I execute an AngularJs method from plain javascript?

myApp.controller("someController",["$scope",function($scope){
  //Some code was here
  $scope.a = valueA;
  $scope.b = valueB
});

And a bit later in the code want to execute a function that when valid could execute an IF and if thats the case will execute my AngularJS controller or what ever.

function Clicked(){
if( was clicked correctly ){
    //execute my Controller and send some data to add to the DOM
}

I don't want my HTML elements to trigger a function inside your controller. I know I can build up my canvas from my AngularJS controller and then, since I'm inside the controller, I will have more easy access to it. But, I wanted to know if there is anyway I could executed from the outside of Angular so I could leave my controller light and simple.

Because; what I want to do it's to connect a small game I have in Phaser Framework with some external elements that I have made in AngularJS. Think about it like a TV just that the bottoms and controllers are in Phaser and the Screen it's the Angular part. So basically when something happens in the Phaser part I want to communicated to the Angular part.

1
  • I don't really have a specific answer, but you might consider getting the $rootScope from your application and using $rootScope.$broadcast('yourevent') to tell your entire application about the click. Commented Aug 28, 2014 at 19:21

1 Answer 1

1

Finally I have a solution,

var intermediary;

myApp.controller("someController", ["$scope",function($scope){

  intermediary = function(fn){
        var phase = $scope.$root.$$phase;
        var value;
        if(phase == '$apply' || phase == '$digest') {
            if(fn && (typeof(fn) === 'function')) {
                value = fn();
                if(value){
                    $scope.valIntermediary = value;
                }
            }
        } else {
            $scope.$apply(function() {
                value = fn();
                if (value) {
                    $scope.valIntermediary = value;
                }
            });
        }
    };

  $scope.$watch("valIntermediary",function(){
      //Do something whit a value var inside my controller
  });
}]};

if(intermediary){
   intermediary(function(value) {
      return { /*Some data when click is true*/}
   })
}
Sign up to request clarification or add additional context in comments.

Comments

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.