Javascript Angular JS:$http.get()数据未更新以查看
Script.jsJavascript Angular JS:$http.get()数据未更新以查看,javascript,angularjs,Javascript,Angularjs,Script.js var app=angular.module("myapp",["panelModule"]); var store=this; store.products=[]; //following code yield proper output // app.controller("StoreController",function () { // this.products=da
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教程。我是安格拉斯的新手。请指出我做错了什么?谢谢
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操作。在成功响应中调用另一个方法并将其打印出来
这就是方法,首先创建一个方法并在其中执行http操作。在成功响应中调用另一个方法并打印出来。试试这个:
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 = [];
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);
});
您使用的浏览器是什么?尝试使用Array.prototype.push.apply(store.products,response.data)代替store.products.push(…response.data);为什么在控制器之外使用
store
属性和onDataReceive
函数?那代码真奇怪,为什么store
在控制器之外?我真的建议你先浏览一下,这样你就能掌握基本知识了。@Paraíso我在用Chrome你在用什么浏览器?尝试使用Array.prototype.push.apply(store.products,response.data)代替store.products.push(…response.data);为什么在控制器之外使用store
属性和onDataReceive
函数?那代码真奇怪,为什么store
在控制器之外?我真的建议你通过考试来掌握基本知识。@Paraíso我在使用Chromei有一个疑问。。。作为成功承诺传递的函数是匿名的。。onDataReceive也应该是响应。如果我错了,请纠正我是的,这是一个输入错误$http.get('js/data.json')。然后(函数(响应){
这里我没有调用onDataReceive函数,它只是响应。编辑并更正..请尝试我有一个疑问…作为成功承诺传递的函数是匿名的。onDataReceive应该是响应。如果我错了,请更正我是的,这是一个输入错误$http.get('js/data.json')。然后(函数(响应){
这里我没有调用OnDaterReceive函数,它只是响应。已编辑并更正..请尝试