0

This is my first time using Angularjs $resource and I am trying to do some testing on my application. I am trying access one of the properties instead of showing the entire object. How can I access data.image or data.url?

json data from weather underground

   {
   "response": {
   "version":"0.1",
    "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
    "features": {
    "conditions": 1
   }
   }
   ,    "current_observation": {
    "image": {
    "url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
    "title":"Weather Underground",
    "link":"http://www.wunderground.com"
    },
    "display_location": {
    "full":"San Francisco, CA"
    }

weather factory

app.factory('weatherService',['$resource', function($resource){
var factory={};
 factory.getWeather = function(){
     return $resource("http://api.wunderground.com/api/9eb7777065b59c55/conditions/q/CA/San_Francisco.json").get();
 }
return factory;
}]);

weather controller

 app.controller('weather', ['$scope', '$http','weatherService',   function($scope, $http, weatherService){
 $scope.weather = weatherService.getWeather().get();
 }]);
1
  • Why you doing .get() twice ? Once inside factory and once inside controller ? Commented Apr 21, 2016 at 5:25

2 Answers 2

1

Your implementation of $resource is wrong, change your factory like this -

app.factory('weatherService',['$resource', function($resource){
var factory={};
factory.getWeather = function(){
     return $resource("http://api.wunderground.com/api/9eb7777065b59c55/conditions/q/CA/:city.json", {city: '@id'});
 }
return factory;
}]);

You should change your controller like this -

 app.controller('weather', ['$scope', '$http','weatherService',   function($scope, $http, weatherService){
     $scope.weather = weatherService
     .getWeather()
     .get({city: 'San_Fransisco'}, function(){
         //other logic
     });
 }]);

$scope.weather has the json object returned from the server

Sign up to request clarification or add additional context in comments.

Comments

0

Perhaps you can use JSON.parse to convert your JSON string to an object and go from there...

 $scope.weather = JSON.parse(weatherService.getWeather().get()); // JSON 
 $scope.weatherUrl = $scope.weather.url;

Comments

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.