[英]Angular JS : $http.get() data is not being updated to View
Script.js
var app=angular.module("myapp",["panelModule"]);
var store=this;
store.products=[];
//following code yield proper output
// app.controller("StoreController",function () {
// this.products=data; //data is a array of object defined in same file
// });
app.controller("StoreController",['$http',function($http){
return $http.get('js/data.json').then(onDataReceive);
}]);
var onDataReceive=function(response) {
store.products.push(...response.data);
console.log(store.products); //this shows [object, object] in browser console
};
我基本上是在视图(index.html)中用ng-repeat遍历对象数组,但是当我使用$ http服务时它没有得到更新。但是静态数据却可以正确显示。 我正在浏览一些在线AngularJs教程。 我是AngularJs的新手。 请指出我在这里做错了什么? 谢谢
尝试这个 :
var app = angular.module("myapp", ["panelModule"]);
app.controller("StoreController", ['$http', function($http) {
var store = this;
store.products = [];
getData = function() {
return $http.get('js/data.json').then(function(response) {
$scope.data = response.data;
});
}
// do the ajax call
getData().then(function(data) {
// stuff is now in our scope, I can alert it
store.products = $scope.data;
console.log(store.products);
});
var app = angular.module("myapp", ["panelModule"]);
app.controller("StoreController", ['$http', function($http) {
var store = this;
store.products = [];
gettingAPIData();
function gettingAPIData() {
$http.get('js/data.json').then(function(response) {
store.products. = response.data;
onDataReceive();
});
}
function onDataReceive() {
console.log(store.products);
};
}]);
这是这样做的方法,您首先创建一个方法并在其中添加http内容。 在成功响应中调用另一个方法并打印出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.