正从角$ HTTP数据而不$范围 [英] getting data from angular $http without $scope
本文介绍了正从角$ HTTP数据而不$范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在试图从$ http.get()
i've been trying to not use $scope when returning data from $http.get()
var app = angular.module('myApp',[])
app.controller('OrderCtrl',['$http',function($http){
this.commande = null
this.orders = {
cde : null,
start : '2014-01-01',
end : '2014-01-31',
get : function(){
return $http.get('http://apimucommerce/api/order/'+this.start+'/'+this.end+'/')
.then(function(response){
return response.data})
}
}
this.commande = this.orders.get()
}])
Ajax调用将返回DATAS,但我不能分配给this.commande财产,什么是错的感谢。
the ajax call is returning datas, but i cannot assign to the this.commande property, what's wrong thanks.
推荐答案
this.orders.get()
返回一个承诺(从$ HTTP),而不是数据。这是因为它是异步的,并需要获取数据它可以返回之前。用于获取数据的正确的模式是:
this.orders.get()
returns a promise (from $http), not data. This is because it's asynchronous, and needs to fetch the data before it can return. The proper pattern for getting the data is:
var app = angular.module('myApp',[])
app.controller('OrderCtrl',['$http',function($http){
var self = this;
this.commande = null
this.orders = {
cde : null,
start : '2014-01-01',
end : '2014-01-31',
get : function(){
return $http.get('http://apimucommerce/api/order/'+this.start+'/'+this.end+'/')
.then(function(response){
return response.data})
}
}
this.orders.get().then(function(data){
self.commande = data;
})
}])
这篇关于正从角$ HTTP数据而不$范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文