当它被设置在函数内部$范围变量未定义 [英] $scope variable is undefined when it is set inside a function

查看:300
本文介绍了当它被设置在函数内部$范围变量未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的学习应用程序下面的示例code。服务做他的工作,并拉起一些数据出来由PHP生成使用JSON code页面,到目前为止,一切顺利。

I have the following example code in my learning app. The service does his job and pulls some data out of a page with json code generated by php, so far so good.

服务:

(function() {
    'use strict';

    angular
        .module('app.data')
        .service('DashboardService', DashboardService);

    DashboardService.$inject = ['$http'];
    function DashboardService($http) {

        this.getFormules = getFormules;

        ////////////////

        function getFormules(onReady, onError) {
            var formJson = 'server/php/get-formules.php',
                formURL  = formJson + '?v=' + (new Date().getTime()); // Disables cash

            onError = onError || function() { alert('Failure loading menu'); };

            $http
                .get(formURL)
                .then(onReady, onError);
        }

    }
})();

然后调用函数getFormules在我的控制器,并把所有数据里面我的$ scope.formuleItems和测试,如果一切成功和o不'... $ scope.formuleItems =未定义<! / code> - 奇怪,因为我的看法是显示数据

控制器的一部分:

    dataLoader.getFormules(function (items){

        $scope.formuleItems = items.data;

    });

    console.log('+++++++++++++++++', $scope.formuleItems); // gives undefined

我做的第一件事就是搜索计算器上到处寻找,如果别人有同样的问题,并且有:<一href=\"http://stackoverflow.com/questions/31371331/undefined-variable-inside-controller-scope-issue\">Undefined变量中控制器功能。

我知道有一些walkarounds对于这一点,我做我自己的研究,但直觉告诉我,这个(见下例)不是解决这个问题的最好办法。

I know there are some walkarounds for this, i've done my own research, but something tells me that this (see example below) isn't the best way to solve this problem.

解决方法一:把$观赏控制器内

$scope.$watch('formuleItems', function(checkValue) {
   if (checkValue !== undefined) {
     //put the code in here!
  }
}

甚至是:

if($scope.formuleItems != null) {}

控制器的其余部分是依靠$ scope.formuleItems。难道我真的把一切都变成了 $观看如果?我可以承诺解决这一问题?我从来没有之前那么一些帮助将AP preciated。

The rest of the controller is relying on $scope.formuleItems. Do i really have to put everything into that $watch or if? Can i fix this with a promise? I never did that before so some help would be appreciated.

推荐答案

在code在回调

function (items){
    $scope.formuleItems = items.data;
}

异步评估。这意味着你首先开火的请求,然后JavaScript的继续执行,你的code线,因此,执行

is evaluated asynchronously. That means you first fire the request, then javascript keeps on executing your lines of code, hence performs

console.log('+++++++++++++++++', $scope.formuleItems); // gives undefined

此时回调尚未被调用,因为这需要一些时间,并且可以在任何时候发生。执行不停止对于这一点。
因此$ scope.formuleItems的价值仍然是不确定的,当然。

At this point the callback was not invoked yet, because this takes some time and can happen at any point. The execution is not stopped for this. Therefore the value of $scope.formuleItems is still undefined, of course.

之后 - 在未来的某些不限定的时间(可能几毫秒后)回调将被调用和$ scope.formuleItems的值将被改变。您需要登录您的回调函数的值INSIDE。

After that - at some not defined time in the future (probably a few milliseconds later) the callback will be invoked and the value of $scope.formuleItems will be changed. You have to log the value INSIDE of your callback-function.

您迫切要理解这个概念,如果你想在JavaScript中获得成功,因为这发生了一遍又一遍:)

You urgently have to understand this concept if you want to succeed in JavaScript, because this happens over and over again :)

这篇关于当它被设置在函数内部$范围变量未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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