简体   繁体   English

AngularJS console.log返回真实数据,然后返回空对象

[英]AngularJS console.log returns true data then empty object

I have a factory: 我有一家工厂:

app.factory("ExampleFactory", function() {
    return {
        map: {}
    }
});

And controller: 和控制器:

app.controller("appCtrl", function($scope, $http, ExampleFactory) {
    $scope.map = ExampleFactory.map;

    $scope.init = function() {
        $http.get("/api") //success
            .success(function(result) {
                $scope.map = exampleMethod(result);
                console.log($scope.map); //{ "1": Object, "2": Object }
            });
        console.log($scope.map); //Object {}
    };

    $scope.init();
});

Why in the first case it returns an array but then returns nothing? 为什么在第一种情况下它返回一个数组却又什么都不返回?

UPDATE: Sorry, there was another problem and it was solved by me. 更新:对不起,还有另一个问题,这个问题我解决了。 I will not delete the answer because I received a correct solutions for this one. 我不会删除答案,因为我收到了正确的答案。

The first case that returns an array is actually executed second. 返回数组的第一种情况实际上是第二个执行的。 The program flow is as follows: 程序流程如下:

  1. $scope.map is set to an empty object by the factory. $scope.map$scope.map设置为空对象。
  2. $scope.init is called $scope.init被称为
  3. $http.get sends an http request to "/api" (but the request is not returned yet) $http.get向“ / api”发送一个http请求(但该请求尚未返回)
  4. $scope.map is printed to console.log , still an empty object $scope.map打印到console.log ,仍然是一个空对象
  5. At some point after this, the web server returns your http request, at which point the .success function is called. 此后,Web服务器将返回您的http请求,然后将.success函数。
  6. exampleMethod sets $scope.map to an array exampleMethod设置$scope.map到一个数组
  7. $scope.map is printed to console.log , at which point it is an array $scope.map打印到console.log ,这是一个数组

I think it should be retuning empty first in the console and then the array. 我认为应该先在控制台中将其调为空,然后在阵列中将其调为空。

This is because the callback that you are passing to the success method is executed asynchronously when the response from the GET /api is received. 这是因为,当收到GET /api的响应时,异步执行要传递给成功方法的回调。 Let's explain it inside your code: 让我们在您的代码中进行解释:

app.controller("appCtrl", function($scope, $http, ExampleFactory) {
    $scope.map = ExampleFactory.map;

    $scope.init = function() {
        // Get request to the /api
        $http.get("/api") //success
            // here we instruct it to use our function if success 
            // but it is not executed until the response is received.
            .success(
                // Callback that is executed on success HTTP 200
                function(result) {
                     $scope.map = exampleMethod(result);

                     // Second console.log displayed after receiving 
                     // the response to the $http.get call
                     console.log($scope.map); //{ "1": Object, "2": Object }
                }
                /////////////////////////////////////////////////
                );

        // First console.log will display empty object
        console.log($scope.map); //Object {}
    };

    $scope.init();
});

Does this makes sense to you? 这对您有意义吗?

Happy coding! 编码愉快!

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

相关问题 当 map 函数在代码中时,它返回 undefined,当 console.log 时,它返回一个空对象和一个数据。 我只想要数据 - when map function is in the code it returns undefined and when console.log it returns the an empty object and an the data. i just want the data console.log返回[object Object] - console.log returns [object Object] 如果语句为真,脚本在块内返回 console.log() 但不返回 - If statement is true, script returns console.log() inside block but not return console.log(array)返回空,但console.log(array.length)不返回0 - console.log(array) returns empty, but console.log(array.length) doesn't return 0 数组上的 JavaScript Async/Await console.log() 返回空 - JavaScript Async/Await console.log() on array returns empty Node.js console.log(object)打印空对象 - Node.js console.log(object) prints empty object 当 object 在多个其他对象中时,console.log 返回 [Object] - console.log returns [Object] when object is in multiple other objects 发送的 axios post api 数据在服务器端的 console.log(req.body) 上给出了空对象 - axios post api data sent gives empty object on console.log(req.body) on server side 为什么节点中的 console.log(this) 返回一个空对象? - Why does console.log(this) in node return an empty object? Javascript console.log返回c()而不是对象的值 - Javascript console.log returns c() instead of the values of the object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM