游戏中的移相器 - JavaScript和事件在AngularJS [英] Game in Phaser - JavaScript and Events in AngularJS
问题描述
如何从普通的JavaScript执行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
});
和稍后在code要执行一个功能,当有效的可以执行IF和如果多数民众赞成的情况下将执行我的AngularJS控制器或什么都。
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
}
我不希望我的HTML元素来触发控制器内部的功能。
我知道我可以从我的AngularJS控制器建立我的画布,然后,因为我的控制器内,我将有更多的轻松访问它。但是,我想知道是否有反正我可以从角的外执行,所以我可以离开我的控制光线和简洁。
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.
由于;我想这样做是在移相器框架与我在AngularJS已经取得了一些外部因素连接一个小游戏我有。想想看,像电视只是底部和控制器都在移相器和屏幕它的折角部分。所以基本上,当东西移相器的一部分发生了,我想传达给折角部分。
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.
推荐答案
最后,我有一个解决方案,
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*/}
})
}
这篇关于游戏中的移相器 - JavaScript和事件在AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!