1

I am trying to use a factory that I can assign to a scoped angular variable. However, whenever I attemp to attach this and use console.log to return this variable I am am returned with the function as text and not as a value. Is there a way I can get this fixed?

========== JS ==========

 annApp.factory('rssFeed', function() { return function() { var events = []; $.get('xmlfile', function(xml) { var json = $.xml2json(xml); jsonb = JSON.stringify(json, undefined, 3 ); // var events = []; var description = json['#document']['rss']['channel']['item']['description']; var title = json['#document']['rss']['channel']['item']['title']; var date = json['#document']['rss']['channel']['item']['pubDate']; events.push({'title': title, 'description':description, 'date': date}); return events; }); return events; } annApp.controller('calendar', ['$scope', 'rssFeed', function($scope, rssFeed){ $scope.events = rssFeed; console.log($scope.events); }]); 
2
  • Are you just looking to call the rssFeed factory and log it to console? In that case you need to invoke the function console.log($scope.events()); Also, I believe the line after return events; should be };}); Commented Nov 13, 2015 at 21:12
  • Here's a fiddle that's correctly returning the function: jsfiddle.net/HB7LU/19871 Commented Nov 13, 2015 at 21:18

1 Answer 1

1

If you're trying to print out the reference to the function itself, then what you have is correct (other than the syntax error mentioned by DenimChicken):

annApp.factory('rssFeed', function(){ return function(){ var events = []; $.get('xmlfile', function(xml){ var json = $.xml2json(xml); jsonb = JSON.stringify(json, undefined, 3 ); var description = json['#document']['rss']['channel']['item']['description']; var title = json['#document']['rss']['channel']['item']['title']; var date = json['#document']['rss']['channel']['item']['pubDate']; events.push({'title': title, 'description':description, 'date': date}); }); return events; }; }); // <-- added this 

If you're trying to print out the return value from the function, then you need to call the function by putting parentheses after the name:

console.log($scope.events()); 

However, since $.get is asynchronous, you should either pass in a callback and call it when the request is complete, or use promises.

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

10 Comments

I was able to get the callback function to work properly using another stackoverflow question found here stackoverflow.com/questions/20369377/….
After being able to create the callback I am not able to display the object data on the page however, I am able to return the object to the console window. Ideas?
Can't say without seeing your code. If you put it in a jsfiddle, post the link and I'll take a look.
You were using the jQuery $.get ajax method, which does not notify Angular that it needs to update its bindings. If you change it to use Angular's $http provider, it will work. (jsfiddle.net/9rv9ahcp/2)
Also, it looks like the URL you're using is in error.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.