【发布时间】:2014-03-06 17:51:21
【问题描述】:
这是我的问题伙计们,
我是 AngularJS 的新手,我做了一个简单的项目列表。我可以搜索这些项目,我什至在里面放了一个分页。
这很好用,我想把我的列表放在我的控制器之外,作为一个 json 文件。
这就是我所做的:
HTML
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Test angular</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript" src="js/controllers.js"></script>
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
</head>
<body >
<section class="app" ng-app="myApp" data-scope="$scope = myApp" >
<div ng-controller="myCtrl" data-scope="$scope = myApp.myCtrl">
<input type="text" ng-model="search" data-scope="$scope = myApp.myCtrl.items(repeater scope)">
Recherche = {{ search }}
<div class="item" ng-repeat="item in newItems | filter:search | startFrom:currentPage*pageSize | limitTo:pageSize">
{{ item.name }}
</div>
<button ng-disabled="currentPage == 0" ng-click="currentPage=currentPage-1">
Previous
</button>
{{currentPage+1}}/{{numberOfPages()}}
<button ng-disabled="currentPage >= items.length/pageSize - 1" ng-click="currentPage=currentPage+1">
Next
</button>
</div>
</section>
</body>
</html>
注意:据我所知,数据范围属性只是为了帮助我了解当前范围
controllers.js
var myApp = angular.module('myApp', []); // creating the module myApp;
// registering startFrom filter
myApp.filter('startFrom', function() {
return function(input, start) {
start = +start; //parse to int
return input.slice(start);
}
});
// configure the module myApp with MyCtrl controller.
myApp.controller('myCtrl', function($scope, $interval, $filter, $http){
$scope.currentPage = 0;
$scope.pageSize = 10;
$scope.numberOfPages=function(){
return Math.ceil($scope.items.length/$scope.pageSize);
}
$scope.$watch('search', function () {
$scope.currentPage = 0;
$scope.newItems = $filter('filter')($scope.items, $scope.search);
$scope.numberOfPages=function(){
return Math.ceil($scope.items.length/$scope.pageSize);
}
});
$scope.newItems = $scope.items;
$http.get('js/items.json') // go get my json file
.then(function(res){
$scope.items = res.data; // set this data to be my items
});
});
json 加载很好(在网络面板中,没问题),但我猜我正在使用的 startFrom 过滤器在 JSconsole 中出现了一些错误:
TypeError: Cannot call method 'slice' of undefined
我的假设是:过滤器会尝试对当前未定义的内容进行切片。就像 $scope.items 是在过滤器完成其工作之后定义的。
编辑:
自从我声明了 $scope.items = []; 之后没有更多错误了正如 VtoCorleone 所说,但现在,真正的问题出现了:列表的第一页没有出现,它只是空的。 但分页有效。
我的建议:我为项目设置了两个属性(items,newItems),items 是原始 json,newItems 是过滤后的结果。使用items,我可以保留我的所有物品,并在需要时恢复它们。
使用 Angular 检查器,在页面加载时,我看到 items 充满了我的 JSON,但 newItems 是空的。(是的,ng-repeat 在 newItems 上 :))为什么是空的?
【问题讨论】:
-
您可以在第一次创建控制器时设置 $scope.items = [] 来设置 currentPage 和 pageSize。那么你将永远不会引用未定义的。
-
没有更多错误,谢谢,但真正的问题仍然存在:在页面加载时,它应该显示分页的第一页(在我尝试将数据放入外部 json 文件),但它没有,我必须开始输入搜索模型以使某些内容出现......
-
我能想到的唯一一件事是你可能在加载json数据后必须使用$compile。
-
最糟糕的是:现在我的 json 是一个字符串,所以 ng-repeat 不再有效。
-
用 angular.fromJson(res.data) 反序列化
标签: javascript ajax json angularjs