Home  >  Q&A  >  body text

angular.js - $http中的 for循环 Cannot read property 'menuName' of undefined

$http.post('sysIndex/getSysIndexNameAndScoreByCid',{cid:id})
            .success(function(resp, status, headers, config){
                var index=resp.data                    
                for(var i=0;i<=index.length;i++){
                    var name=index[i].menuName;                                        
                }                  
            })


初学angular 为什么会报这个错


index的值是这样的。。

滿天的星座滿天的星座2713 days ago607

reply all(3)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-15 16:59:32

    You need to set a breakpoint to see what the retrieved index is


    I know, you wrote it while looping

    for(var i=0;i<=index.length;i++)

    should be

    for(var i=0;i<index.length;i++){

    There is one more equal sign. So the last item is undefined, so there is no menuName attribute. . .

    reply
    0
  • PHP中文网

    PHP中文网2017-05-15 16:59:32

    This is a problem with your background data.

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-15 16:59:32

    The problem here should not be that every element under the index is undefined. In the screenshot of the questioner, only the first element is normal. Try changing it to the following:

    var name = !!index[i]?index[i].menuName:'';

    reply
    0
  • Cancelreply