Experiencing difficulties retrieving service data to include in the variable's scope. Below is my controller code:
'use strict';
var app = angular.module('ezcms2App.controllers', []);
app.controller('NoticiaCtrl', ['$scope', 'NoticiasFactory', '$http',
function ($scope, NoticiasFactory, $http) {
//Testing paging on a local array - it works $scope.makeTodos = function () { $scope.todos = []; for (var i = 1; i <= 20; i++) { $scope.todos.push({text: 'todo ' + i, done: false}); } //The following does not work, $scope.todos ends up being undefined /$scope.todos = NoticiasFactory.query(function (entries) { for (var i in entries) { $scope.todos.push(entries[i); } });/ };
$scope.makeTodos();
$scope.totalItems = $scope.todos.length;
$scope.filteredTodos = []
, $scope.currentPage = 1
, $scope.numPerPage = 10
, $scope.maxSize = 5;
$scope.numPages = function () {
return Math.ceil($scope.todos.length / $scope.numPerPage);
};
$scope.$watch('currentPage + numPerPage', function () {
var begin = (($scope.currentPage - 1) * $scope.numPerPage)
, end = begin + $scope.numPerPage;
$scope.filteredTodos = $scope.todos.slice(begin, end);
});
}]);
I also attempted using http get but as it is not asynchronous, it also returns undefined
'use strict';
var app = angular.module('ezcms2App.controllers', []);
app.controller('NoticiaCtrl', ['$scope', 'NoticiasFactory', '$http',
function ($scope, NoticiasFactory, $http) {
$scope.makeTodos = function () {
$http.get('http://localhost:9292/localhost:80/apiadmin/index.php/api/noticia').success(function (resp) {
$scope.todos = resp.data;
});
};
$scope.makeTodos();
$scope.totalItems = $scope.todos.length;
$scope.filteredTodos = []
, $scope.currentPage = 1
, $scope.numPerPage = 10
, $scope.maxSize = 5;
$scope.numPages = function () {
return Math.ceil($scope.todos.length / $scope.numPerPage);
};
$scope.$watch('currentPage + numPerPage', function () {
var begin = (($scope.currentPage - 1) * $scope.numPerPage)
, end = begin + $scope.numPerPage;
$scope.filteredTodos = $scope.todos.slice(begin, end);
});
}]);