2015-09-10 151 views
0

我對vue非常陌生。我試圖執行一個簡單的for循環,但由於某種原因,它不工作。任何幫助將不勝感激。我的代碼:不能在vue.js中執行for循環

var Vue = require('vue'); 
Vue.use(require('vue-resource')); 
Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('value'); 
new Vue({ 

    el: '#adresponse', 

    ready: function() { 
     this.fetchMessages(); 
    }, 

    data: { 
     classified_bids: {}, 
     accept_qty: {}, 
     submitted: false 
    }, 

    methods: { 
     fetchMessages: function() { 
      this.$http.get('/api/getbids') 
       .success(function (bids) { 
        this.classified_bids = bids; 
        for (i = 0; i < this.classified_bids.length; i++) { 
         this.accept_qty[i] = 0; 
        } 
       }); 
     } 
    } 
}); 

回答

0

你的問題是this.success()回調中的價值。

你可以嘗試這樣的事:

this.$http.get('/api/getbids') 
    .success(function (bids) { 
    this.classified_bids = bids; 
    for (i = 0; i < this.classified_bids.length; i++) { 
     this.accept_qty[i] = 0; 
    } 
    }.bind(this)); 

Similar problem

1

通過改變像這樣循環,這工作:

el: '#adresponse', 

ready: function() { 
    this.fetchMessages(); 
}, 

data: { 
    classified_bids: {}, 
    accept_qty: {}, 
    submitted: false 
}, 

methods: { 
    fetchMessages: function() { 
     this.$http.get('/api/getbids') 
      .success(function (bids) { 
       this.classified_bids = bids; 
       for (var key in this.classified_bids) { 
        this.accept_qty[key] = 0; 
       } 
      }); 
    } 
} 

});