1

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?

3
  • Can you please tell us what error you get or how $scope looks like ? Commented Sep 26, 2016 at 12:53
  • getting any eorrs?when i checked with both cases i am able to see the day.... Commented Sep 26, 2016 at 12:57
  • I don't check any errors in browser console Commented Sep 26, 2016 at 13:04

2 Answers 2

2

$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;
       });
}
Sign up to request clarification or add additional context in comments.

Comments

1

I think, this will be better (from $resource doc):

function IndexController($scope, NewsService) {
    $scope.result = {};

    NewsService.get({}, function (data) {
      $scope.result = data;
    });
};

and in template {{ result.day }}

2 Comments

But how to get normal Java Script object from $resource object, because i need to have access to property of it for example "day" in JS code .
NewsService.get - async function and it need callback, where you can operrate with result. $scope.result - is the normal object and also it can be use into template directly. In callback you can do anything, like console.log($scope.result.day)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.