I am fairly new to Angular development and I've hit a roadblock that's been holding me back for a while.
My factory is successfully creating an array filled with objects, specifically RSS feed data, and I can see it loading in the console. However, I'm struggling to get this array to return to the controller.
Any assistance you can provide would be greatly appreciated. Please excuse the messy 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);