如何获得角函数外的变量值 [英] How to get variable value outside angular function

查看:107
本文介绍了如何获得角函数外的变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经花了数小时的时间去了解为什么我不能在Angular.js函数之外获得变量的值。首先我从Firebase数据库中获取值,这里是我的参考:

$ p $ var $ refspaedtSe​​rv = new Firebase(FBURLSPA + $ routeParams。 ID);
$ scope.spaedServ = $ firebaseObject(refspaedtSe​​rv);

然后我有函数:

  /////图片参考//// 
var lafoto ='';
refspaedtSe​​rv.on(value,function(rootSnapshot){
lafoto = rootSnapshot.val()。foto;
console.log(Inside image,lafoto)
});

正如您所看到的,我将变量'lafoto'定义为全局
使用控制台.log里面的图像,我可以看到值是正确的

但是当我试图获得lafoto变量的值,在功能外,我得到

  console.log(Outside Image,lafoto)

这看起来很愚蠢,但是我正为此疯狂。
有人可以给我一个提示吗?

问候,
Victor

解决方案

非常简单,让我们看看你的代码,假设你的函数在'value'事件中需要1500ms完成:

  var lafoto =''; 
console.log('start');

//监听事件
refspaedtSe​​rv.on(value,function(rootSnapshot){
// after〜1500ms ...
lafoto = rootSnapshot .val()。foto;
console.log(Inside,lafoto)
});

console.log('outside',lafoto); //立即执行

在控制台中,您将收到结果:

 '开始'
'在'$ b $'之外'

这是因为你在等待异步事件:因此,外的.on函数你执行代码之前

所以,您的变量lafoto总是会导致undefined,因为它还没有被分配。





编辑1



您可以使用回调函数在.on事件之后执行代码:

  var lafoto =''; 
console.log('start');

//监听事件
refspaedtSe​​rv.on(value,function(rootSnapshot){
// after〜1500ms ...
lafoto = rootSnapshot .val()。foto;
console.log(Inside,lafoto)
myAfterFunction();
});

函数myAfterFunction(){
console.log('myAfterFunction',lafoto);
}

console.log('outside',lafoto); //立即执行

在控制台中,您将收到结果:

 '开始'
'外部'$ b $'内'
'myAfterFunction'


I have been hours trying to understand why I don't get the value of a variable, outside an Angular.js function. First I get the value from a Firebase database, here is my reference :

var refspaedtServ = new Firebase(FBURLSPA + $routeParams.id);
$scope.spaedServ = $firebaseObject(refspaedtServ);

Then I have the function:

        /////  IMAGE REFERENCE  ////
        var lafoto = ''; 
        refspaedtServ.on("value", function(rootSnapshot) {
           lafoto = rootSnapshot.val().foto;
           console.log("Inside image ", lafoto)
       });

As you can see, I define my variable 'lafoto' as global With the console.log Inside image, I can see the value is correct

But when I try to get the value of "lafoto" variable, outside the function, I'm getting "undefined", I mean no value.

console.log("Outside Image ", lafoto)

It seems silly, but I'm reaching madness for that. Can anybody give me a hint please?

Regards, Victor

解决方案

It's pretty simple, let's see it by your code, assuming your function in the 'value' event needs 1500ms to complete:

var lafoto = '';
console.log('start');

//listening for the event
refspaedtServ.on("value", function(rootSnapshot) {
    //after ~1500ms...
    lafoto = rootSnapshot.val().foto;
    console.log("Inside", lafoto)
});

console.log('outside', lafoto); //executed immediately

In console, you will receive as result:

'Start'
'outside'
'inside'

that's because you are waiting asynchronously for the event: hence, "outside" the .on function you are executing the code before the code inside.

So, your variable lafoto will always result undefined, because it has not been already assigned.

.

Edit 1

You can use a callback function to perform code after the .on event:

 var lafoto = '';
console.log('start');

//listening for the event
refspaedtServ.on("value", function(rootSnapshot) {
    //after ~1500ms...
    lafoto = rootSnapshot.val().foto;
    console.log("Inside", lafoto)
    myAfterFunction();
});

function myAfterFunction(){
    console.log('myAfterFunction', lafoto);
}

console.log('outside', lafoto); //executed immediately

In console, you will receive as result:

'Start'
'outside'
'inside'
'myAfterFunction'

这篇关于如何获得角函数外的变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆