luafanti luafanti - 2 months ago 18
Javascript Question

Get property value from $resource object AngularJS

I am beginner in AngularJS but i must modify little module for my project.
Here is my service factory:

services.factory('NewsService', function($resource) {

var result = $resource('http://localhost:8090/boiler/1');
return result;
});


Here i use it:

function IndexController($scope, NewsService) {

var result = NewsService.get()

$scope.xxx= result.day;
$scope.www = result;


I don't understand why I can send to $scope variable
result
and use it in html as
{{www.day}}
but i can't send to $scope property of variable
result.day
and use it as
{{xxx}}


This is my Json response from server

{
"day" : "2016-10-06",
"amountOfEnergy" : 40,
"cost" : 120,
"content" : [ ],
"links" : [ {
"rel" : "self",
"href" : "http://localhost:8090/boiler/1"
}, {
"rel" : "bathroom",
"href" : "http://localhost:8090/boiler/1"
} ]
}


Somebody can explain me it?

Answer

$resource return promise so use $promise.then in controller

function IndexController($scope, NewsService) {
    NewsService.get()
       .$promise
       .then(function(response){
            console.log(response);
            var result = response;
            $scope.xxx= result.day;
            $scope.www = result;
       });
}
Comments