简体   繁体   English

Phaser中的游戏-AngularJS中的JavaScript和事件

[英]Game in Phaser - JavaScript and Events in AngularJS

How can I execute an AngularJs method from plain javascript? 如何从纯JavaScript执行AngularJs方法?

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. 稍后在代码中要执行一个函数,该函数在有效时可以执行IF,如果是这样,该情况将执行我的AngularJS控制器或其他任何函数。

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. 我不希望我的HTML元素触发您控制器内的函数。 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. 我知道我可以从AngularJS控制器构建画布,然后,由于我位于控制器内部,因此可以更轻松地访问它。 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. 但是,我想知道是否仍然可以从Angular的外部执行,这样我的控制器就可以变得轻便简单。

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. 我要做的是将我在Phaser Framework中拥有的一个小游戏与我在AngularJS中制作的一些外部元素连接起来。 Think about it like a TV just that the bottoms and controllers are in Phaser and the Screen it's the Angular part. 像电视一样思考它,只是底部和控制器在Phaser中,而Screen是Angular部分。 So basically when something happens in the Phaser part I want to communicated to the Angular part. 因此,基本上,当移相器部分发生问题时,我想与角度部分进行通信。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM