[英]Angular JS : $http.get() data is not being updated to View
Script.js 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
};
I m basically iterating over the array of object with ng-repeat in my view( index.html) But it is not getting update when i used $http service.However static data is getting displayed properly. 我基本上是在视图(index.html)中用ng-repeat遍历对象数组,但是当我使用$ http服务时它没有得到更新。但是静态数据却可以正确显示。 I m going thru some online AngularJs tutorial.
我正在浏览一些在线AngularJs教程。 I m new to AngularJs.
我是AngularJs的新手。 Please point me out what im doing wrong here?
请指出我在这里做错了什么? Thanks
谢谢
Try this : 尝试这个 :
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);
};
}]);
This is the way to do it, you first create a method and do the http stuff in it. 这是这样做的方法,您首先创建一个方法并在其中添加http内容。 Call another method in the success response and print it out.
在成功响应中调用另一个方法并打印出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.