分页angularjs $ scope变量未定义

时间:2015-01-20 15:50:13

标签: javascript angularjs paging

我无法恢复服务数据以插入变量范围。 我的ctrl:

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

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

//测试分页本地数组 - 它的工作原理     $ scope.makeTodos = function(){         $ scope.todos = [];         for(var i = 1; i< = 20; i ++){             $ scope.todos.push({text:'todo'+ i,done:false});         }      //它没有用,$ scope.todos = undefined     /$scope.todos= NoticiasFactory.query(function(entries){      for(var i in entries){             $ scope.todos.push(条目[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);
});

}]);

我也尝试过通过http获取,以及如何不同步他也提供了未定义的

 '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);
        });


    }]);

1 个答案:

答案 0 :(得分:0)

解决:

'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);
            });
        });

    }]);