Paginate,自定义滤镜,AngularJS

时间:2015-05-26 04:10:29

标签: angularjs paginate

我正在创建一个带有一些嵌入式html的对象列表,我添加了一个过滤器,但是由于他们看到了什么错误,我想把我扔掉。

这是分页的示例:PLUNKR

**

应该显示一个包含寻呼的列表并应用过滤器。

**

angular.module('App', ['ui.bootstrap','ngResource'])
.controller('mainCtrl',['$scope',
	function($scope){
		$scope.items=[
			{id: '1', title: '<b>Chicago</b>'},
			{id: '2', title: '<b><i>New York</i></b>'},
			{id: '3', title: '<div><p>London</p></div>'},
			{id: '4', title: '<div><p>Washington</p></div>'},
			{id: '5', title: '<div><p>Hawai</p></div>'},
			{id: '6', title: '<div><p>Dinamarca</p></div>'},
			{id: '7', title: '<div><p>Peru</p></div>'},
			{id: '8', title: '<div><p>USA</p></div>'},
			{id: '9', title: '<div><p>Ecuador</p></div>'},
			{id: '10', title: '<div><p>Japon</p></div>'}
		];

		$scope.totalItems = $scope.objects.length;
		$scope.currentPage = 1;
		$scope.numPerPage = 5;

		$scope.paginate = function(value) {
			var begin, end, index;
			begin = ($scope.currentPage - 1) * $scope.numPerPage;
			end = begin + $scope.numPerPage;
			index = $scope.objects.indexOf(value);
			return (begin <= index && index < end);
		};
	}
])

.filter("htmlToPlaintext", function() {
	return function(text) {
	return text.replace(/<[^>]+>/gm, '');
	}
});
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://code.angularjs.org/1.3.15/angular.js"></script>
<script src="https://code.angularjs.org/1.3.15/angular-resource.js"></script>
<script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.0.js"></script>
<div ng-app="App">
	<div ng-controller="mainCtrl">
		<div ng-repeat="item in items | filter : paginate">
			{{item.title|htmlToPlaintext}}
		</div>
		<pagination total-items="totalItems" ng-model="currentPage"
	max-size="5" boundary-links="true"
	items-per-page="numPerPage" class="pagination-lg">
	</pagination>
	</div>
</div>

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题(我确实修改了PLUNKR,你可能想要删除html元素并只想要文本内)。你的过滤器,它的编写方式,将删除所有内容并返回&#34;&#34;。假设过滤器的输入将是html内容,你想要显示html的感知文本。

尝试以下

 .filter("htmlToPlaintext", function() {
    return function(text) {
       var htmlNode = document.createElement('div'); 
       htmlNode.innerHTML = text;
       return htmlNode.innerText;
    }
});