从angularjs中的ngResource访问控制器内的变量值
我对一个API进行Get调用,在我的控制器中,我想访问该对象的值,我该怎么做?如果我使用数据绑定,我可以查看该对象中变量的值从angularjs中的ngResource访问控制器内的变量值,angularjs,ngresource,Angularjs,Ngresource,我对一个API进行Get调用,在我的控制器中,我想访问该对象的值,我该怎么做?如果我使用数据绑定,我可以查看该对象中变量的值 var app = angular.module("myApp",["ngResource"]); app.factory("myAppFactory",function($resource){ return{ Test: $resource("https://jsonplaceholder.typicode.com/users/:id", {id:"@id"
var app = angular.module("myApp",["ngResource"]);
app.factory("myAppFactory",function($resource){
return{
Test: $resource("https://jsonplaceholder.typicode.com/users/:id", {id:"@id"})
}
});
app.controller("ctrl",function($scope,myAppFactory){
var productDetailServer = myAppFactory.Test.get({ id: 1 }, function () {
});
var aux=productDetailServer;
$scope.variable = productDetailServer;
console.log(productDetailServer);
console.log("the website\n");
console.log(aux.website);
console.log("how do you access variable inside the response?");
/*
How can I do something like this:
var website =productDetailServer.website
website should access the website within the productDetailServer object but
all I get is undefined, how do I assign the website propierty from the object to the variable?
Thanks
*/
});
但是如何从控制器访问变量的值呢?谢谢
为了更好地解释我的意思,我做了一个简单的解释:
它返回一个承诺,你应该使用它
myAppFactory.Test.get({ id: 1 }).$promise.then(function (data) {
$scope.variable = data;
});
你应该读书