无法访问由JavaScript函数在AngularJS返回的对象元素 [英] Cannot access elements in object returned by Javascript Function in AngularJS

查看:135
本文介绍了无法访问由JavaScript函数在AngularJS返回的对象元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于@asgoth,我能够使用AngularJS $ http服务从雅虎检索股票价格如下所述:<一href=\"http://stackoverflow.com/questions/13963192/cannot-read-response-from-angularjs-resource-jsonp-get-from-yahoo-finance\">Cannot阅读AngularJS $资源响应JSONP从雅虎财经获得

Thanks to @asgoth, I am able to use AngularJS $http service to retrieve stock prices from Yahoo as described here: Cannot read response from AngularJS $resource JSONP get from Yahoo Finance

在getHistoricalPrice功能,它把价格的阵列,它是一个对象内的内部。从这个函数里,我能够访问该价格,写安慰。

In the "getHistoricalPrice" function, it puts the price inside an array, which is inside an object. From inside that function, I am able to access the price and write it to console.

该函数返回该对象,在那里它从调用。从那里,我可以成功写入整个对象到控制台。但是,我不能访问这个对象的元素。我试过很多不同的方式,但还是在对象无法访问数据。你可以看到code。在 http://jsfiddle.net/curt00/LTazR/2/ 或如下:

The function returns the object to where it is called from. From there, I can successfully write the entire object out to console. However, I cannot access the elements of this object. I tried many different ways, but still cannot access the data in the object. You can see the code at http://jsfiddle.net/curt00/LTazR/2/ or below:

angular.module('app', ['ngResource']);

function AppCtrl($scope, $http, $resource) {

var historical_price = getHistoricalPrice("AAPL", 'start date is hard coded', 'end date is hard coded');
console.log("after calling historical price: ", historical_price);  // historical_price is an object and all of the correct data is outputted to console here, but I cannot access its elements directly from Javascript.

for(var key in historical_price) {
    console.log("key =",key);  // this outputs "key = list"
}

console.log("after calling getHistoricalPrice: ", historical_price.list[0][1]);  // Cannot access this as browser console gives error: TypeError: Cannot read property '1' of undefined
console.log("after calling getHistoricalPrice: ", historical_price['list'][0][1]);  // Cannot access this as browser console gives error: TypeError: Cannot read property '1' of undefined
console.log("after calling getHistoricalPrice: ", historical_price[0][1]);  // Cannot access this as browser console gives error: TypeError: Cannot read property '1' of undefined


function getHistoricalPrice(symbol, start, end) {

    var query = 'select * from csv where url=\'http://ichart.yahoo.com/table.csv?s=' + symbol + '&a=' + '11' + '&b=' + '19' + '&c=' + '2012' + '&d=' + '11' + '&e=' + '19' + '&f=' + '2012' + '&g=d&ignore=.csv\'';


    var url = 'http://query.yahooapis.com/v1/public/yql?q=' + fixedEncodeURIComponent(query) + '&format=json&callback=JSON_CALLBACK';

    var histData = {};

    $http.jsonp(url, {timeout: 30000}).success(function(json) {
        var list = [];


        var result = json.query.results.row;

        result.shift(); // remove the header (columns) row
        angular.forEach(result, function(row) {
            list.push([(new Date(row.col0)).getTime()/1000, parseFloat(row.col4)]);

        });
        list.sort(function(val1, val2) {
            return val1[0] - val2[0];
        });
        histData.list = list;
        console.log('Loaded historical data',histData.list[0][1],', for ' + symbol);  // This works and gives the price
    });

    return histData;
}

var fixedEncodeURIComponent = function(str) {
    return encodeURIComponent(str).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
};



}

结果
任何帮助或建议来解决这个问题是很大的AP preciate!


Any help or suggestions to solve this problem is greatly appreciate!

推荐答案

感谢所有提供建议。

我用AngularJS'$范围变量,如$ scope.symbol [用户]。价格解决了这个问题。我调用getHistoricalPrice函数,然后在该函数之前创建这个变量,结果是从$ http.jsonp返回之后,我把价值到$作用域变量,因为这样的:

I solved the problem by using AngularJS' $scope variable, such as $scope.symbol[user].price. I created this variable before calling the getHistoricalPrice function and then in that function, after the result is returned from $http.jsonp, I put the value into the $scope variable, as such:

$scope.symbol[user].price = row.col4;

这篇关于无法访问由JavaScript函数在AngularJS返回的对象元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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