为范围变量

时间:2017-07-06 12:49:58

标签: angularjs ajax rest

我已经在下面的模拟数据上开发了我的应用程序。

$scope.products=[{serial:1,quantity:30,name:"aaa"},
                 {serial:2,quantity:10,name:"bbb"}];

我还准备了从数据库中获取数据并在http://localhost:8080/myrests/shop/productresource

处获取的其余资源

是否可以帮助我如何将此资源返回结果分配给上述$scope.products

1 个答案:

答案 0 :(得分:0)

如果你想异步使用promis
    //在函数内部的代码包装

var deferred = $q.defer();
$http.get(URL) 
.then(function(data, status, headers, config) {
    deferred.resolve(data);
    console.log("Success " + data);
},
function(data, status, headers, config) {
    deferred.reject(data);
    console.log("Failure " + status);
});
return deferred.promise;

否则只需使用$ http

$http.get("http://localhost:8080/myrests/shop/productresource") 
.then(function(data, status, headers, config) {
    $scope.products = data;
},
function(data, status, headers, config) {
    console.log("Failure " + status);
});
相关问题