代码之家  ›  专栏  ›  技术社区  ›  Sandra Willford

angularjs函数返回的数组是一个对象,而不是字符串

  •  0
  • Sandra Willford  · 技术社区  · 7 年前

    我在让下面的函数作为可用字符串返回时遇到了一些问题?

    app.factory('Urls', ['$http', function($http) {
        var urls = {};
        urls.getUrls = function () {
            return  $http.get('json/dataUrls.json'); 
        }
        return urls
    }]);
    
    app.factory('Emails', ['$http', 'Urls', function($http, Urls) {
    
        var dataUrl = Urls.getUrls().then(function(response) {
            return response.data.emails;
        });
        console.log(dataUrl);
    
        var query = {};
        query.getItems = function() {
            return  $http.get('json/emails.json');  
        };
        return query;
    }]);
    

    以下是 console.log(dataUrl); 我想得到的是值字符串。。。

    f {$$state: {…}}
    $$state
    :
    status
    :
    1
    value
    :
    "json/emails.json"
    __proto__
    :
    Object
    __proto__
    :
    Object
    
    3 回复  |  直到 7 年前
        1
  •  2
  •   Derek Brown Onga Leo-Yoda Vellem    7 年前

    Urls.getUrls().then(...) 返回承诺( read more about this here ),一种数据类型,用于保存对异步获取的值的“引用”。因此,运行 console.log 在…上 dataUrl 将返回一个对象,而不是字符串。

    为了获得这个值,您需要异步访问它。最好的方法是通过 promise chaining :

    app.factory('Urls', ['$http', function($http) {
        var urls = {};
        urls.getUrls = function () {
            return  $http.get('json/dataUrls.json'); 
        }
        return urls
    }]);
    
    app.factory('Emails', ['$http', 'Urls', function($http, Urls) {
    
        Urls.getUrls().then(function(response) {
            return response.data.emails;
        }).then(function(emails){
            console.log(emails);
        });
    
        var query = {};
        query.getItems = function() {
            return  $http.get('json/emails.json');  
        };
        return query;
    }]);
    
        2
  •  1
  •   Daniel Tran    7 年前

    您从中获取的数据URL var dataUrl = Urls.getUrls().then(... 实际上是一个承诺,而不是字符串。我猜你的意图有点像贝娄:

    app.factory('Urls', ['$http', function($http) {
        var urls = {};
        urls.getUrls = function () {
            return  $http.get('json/dataUrls.json'); 
        }
        return urls
    }]);
    
    app.factory('Emails', ['$http', 'Urls', function($http, Urls) {
    
        var query = {};
        query.getItems = function() {
            return Urls.getUrls().then(function(response) {
                return response.data.emails;
            }).then(dataUrls => { // dataUrls here is a string, you can use it.
                return  $http.get(dataUrls); 
            });
        };
        return query;
    }]);
    
        3
  •  0
  •   Shree Wankhede    7 年前
    app.factory('Urls', ['$http', function($http) {
        var urls = {};
        urls.getUrls = function () {
            return  $http.get('json/dataUrls.json'); 
        }
        return urls
    }]);
    
    app.factory('Emails', ['$http', 'Urls', function($http, Urls) {
    
        Urls.getUrls().then(function(response) {
            return response.data.emails;
        }).then(function(emails){
            console.log(emails);
        });
    
        var query = {};
        query.getItems = function() {
            return  $http.get('json/emails.json');  
        };
        return query;
    }]);