我有一個數組對象的函數。在angularJs中將ajax響應推送到數組中
$http({
method: 'GET',
url: 'select.php'
}).then(function success(response) {
alert(response);
//$scope.posts=response.data;
}, function error(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
}
);
function store() {
this.products = [
new product("APL", "Apple", "Eat one every…", 12, 90, 0, 2, 0, 1, 2),
new product("AVC", "Avocado", "Guacamole…", 16, 90, 0, 1, 1, 1, 2),
new product("BAN", "Banana", "These are…", 4, 120, 0, 2, 1, 2, 2)
];
}
在上面的代碼中我們傳遞一些靜態數據,而是我試圖推它來自另一個文件作爲Ajax響應一些動態數據。
所以我怎麼能使它動態。我已經試過這樣
function store() {
this.products = [
//new product("APL", "Apple", "Eat one every…", 12, 90, 0, 2, 0, 1, 2),
//new product("AVC", "Avocado", "Guacamole…", 16, 90, 0, 1, 1, 1, 2),
//new product("BAN", "Banana", "These are…", 4, 120, 0, 2, 1, 2, 2)
$scope.product.push(response.data);
];
}
但它不工作。任何想法我怎麼能做到這一點?
你必須把$ scope.product.push(response.data);在ajax請求 – Vivz
$ scope.products而不是$ scope.product的情況下,如果你沒有注意到新產品()的錯字 – rrd
,這個應該重複取決於總行數據的權利? –