2017-04-11 128 views
0

我在控制器內部調用一個API的Get調用我想訪問該對象的值我該怎麼做?如果使用數據綁定,我可以查看該值對象內的變量。在angularjs中從ngResource訪問控制器內的變量值

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 

    */ 
}); 

但我怎麼能訪問來自控制器的變量的值?由於

我做了一個Plunker更好地解釋我的意思:

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

回答

相關問題