jit*_*hin 1 angularjs angularjs-service
var newservices = angular.module('newservices', []);
newservices.service('newservice', function ($http) {
return{
newdata: function(parameter){
return $http.get('/devicedetails/'+parameter).success(function(data) {
console.log(data)
return data
});
},
}
});
Run Code Online (Sandbox Code Playgroud)
上述服务包含在我的一个控制器中
data=newService.newdata($scope.dummy)
console.log(data)
Run Code Online (Sandbox Code Playgroud)
我试图打印数据时得到的是$ http函数对象,如下所示
Object {then: function, catch: function, finally: function, success: function, error: function}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
你看到的不是错误.约定了.你做了一个$httpGET请求,这是异步的.$http.get返回在远程请求完成时将解析的promise.在那一刻,你将获得最终价值.
看看这个例子,getShops你的方法在哪里newData
this.getShop = function (id, lang) {
var promise = $http.get(appRoot + 'model/shops_' + lang + '.json');
return promise;
};
Run Code Online (Sandbox Code Playgroud)
在控制器中,您可以像这样使用它:
Shops.getShop($routeParams.id).then(function (response) {
console.log("data is", response.data);
$scope.shop = response.data[$routeParams.id];
});
Run Code Online (Sandbox Code Playgroud)
数据准备就绪后,将其分配给范围.
在你的情况下:
var data;
newService.newdata($scope.dummy).then(function (response) {
data = response.data;
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14150 次 |
| 最近记录: |