简体   繁体   中英

Paging angularjs $scope variable is undefined

I'm having trouble recovering service data to insert into the scope of variable. My ctrl:

'use strict';
var app = angular.module('ezcms2App.controllers', []);

app.controller('NoticiaCtrl', ['$scope', 'NoticiasFactory', '$http',
function ($scope, NoticiasFactory, $http) {            

//Test paging local array - It's work $scope.makeTodos = function () { $scope.todos = []; for (var i = 1; i <= 20; i++) { $scope.todos.push({text: 'todo ' + i, done: false}); } //It's not working, $scope.todos = 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 tried via http get and how not asynchronous he gives also 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);
        });


    }]);

Resolved:

'use strict';
var app = angular.module('ezcms2App.controllers', []);

app.controller('NoticiaCtrl', ['$scope', 'NoticiasFactory', '$http',
    function ($scope, NoticiasFactory, $http) {
        //$scope.noticias = NoticiasFactory.query();

        $scope.noticias = [];
        NoticiasFactory.query(function (entries) {
            $scope.noticias = entries;

            $scope.totalItems = $scope.noticias.length;

            $scope.filteredNoticias = []
                    , $scope.currentPage = 1
                    , $scope.numPerPage = 10
                    , $scope.maxSize = 5;



            $scope.numPages = function () {
                return Math.ceil($scope.noticias.length / $scope.numPerPage);
            };

            $scope.$watch('currentPage + numPerPage', function () {
                var begin = (($scope.currentPage - 1) * $scope.numPerPage)
                        , end = begin + $scope.numPerPage;

                $scope.filteredNoticias = $scope.noticias.slice(begin, end);
            });
        });

    }]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM