0

I make a Get call to an API, inside my controller I want to access the values of that object how can I do it?, if I use data binding I can view the value of the variable within the object.

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

    */
});

But how can I access the value of the variables from the controller? Thanks

I made a Plunker to better explain what I meant:

https://plnkr.co/edit/lSsxdbgc5eRXzuIV64ND?p=preview

Angel Silva
  • 365
  • 2
  • 5
  • 16

1 Answers1

0

It returns a promise, you should use it.

myAppFactory.Test.get({ id: 1 }).$promise.then(function (data) {
  $scope.variable = data;
});

Updated Plunker

You should read How do I return the response from an asynchronous call?

Community
  • 1
  • 1
Satpal
  • 132,252
  • 13
  • 159
  • 168