I am just starting out in Angular, and I have been stuck for some time on an issue.
My factory is properly creating an array containing objects, RSS feed data, and will load to the console. The issue is that I cannot seem to get the array to return to the controller.
Any help would be appreciated. Please pardon the rough and sloppy code.
Main.js
function FeedCtrl($scope, GetFeed) {
$scope.itemId = 0;
//$scope.items = [];
console.log('Get episodes in ctrl');
$scope.items = function() {
return GetFeed.getEpisodes();
};
console.log('display items from ctrl');
console.log($scope.items());
$scope.itemId = function(index) {
$scope.itemId = index;
console.log($scope.itemId);
console.log($scope.itemNames[$scope.itemId].title);
};
};
function EpisodeCrtl($scope, GetFeed) {
//getFeed.pullFeed();
};
function GetFeed($http){
var episodeArray = [];
function items(){
console.log('Firing pullFeed');
return $http.get('assets/feed.xml').then(function(response) {
var x2js = new X2JS()
var itemDef = x2js.xml_str2json(response.data);
itemsObj = itemDef.rss.channel.item;
var numOfItems = itemsObj.length;
episodeArray.length = 0;
for (var i = 0; i < numOfItems; i++) {
episodeArray.push({
title: itemsObj[i].title,
link: itemsObj[i].link,
author: itemsObj[i].author,
pubDate: new Date(itemsObj[i].pubDate),
summary: itemsObj[i].summary,
duration: itemsObj[i].duration,
description: itemsObj[i].description
});
}
console.log(episodeArray);
return episodeArray;
})
};
return {
getEpisodes: function(){
console.log(episodeArray);
episodeArray.length = 0;
items();
console.log(episodeArray);
return(episodeArray);
}
}
};
var app = angular.module('ctApp', ['ngMaterial', 'ngAnimate', 'ngRoute'])
.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'list.html',
controller: 'FeedCtrl'
})
.when('/about', {
templateUrl: 'about.html',
controller: 'EpisodeCrtl'
});
})
.config( [ '$compileProvider', function( $compileProvider ) {
var currentImgSrcSanitizationWhitelist = $compileProvider.imgSrcSanitizationWhitelist();
var newImgSrcSanitizationWhiteList = currentImgSrcSanitizationWhitelist.toString().slice(0,-1)
+ '|chrome-extension:'
+currentImgSrcSanitizationWhitelist.toString().slice(-1);
}
])
.factory('GetFeed', GetFeed)
.controller('FeedCtrl', FeedCtrl)
.controller('EpisodeCrtl', EpisodeCrtl);