Julian
Julian

Reputation: 33

Game in Phaser - JavaScript and Events in AngularJS

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.

Upvotes: 1

Views: 1050

Answers (1)

Julian
Julian

Reputation: 33

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*/}
   })
}

Upvotes: 1

Related Questions