【发布时间】:2015-08-03 10:42:14
【问题描述】:
我制作了一个具有隔离范围的自定义指令,并且也在该指令中获取数据,但现在我的过滤器和排序在这里不起作用是我的指令:
<div my-data remoteurl='url' filter='test'>
</div>
控制器:
(function() {
'use strict';
var myApp = angular.module('myApp', [])
.controller('myAppCtrl', ['$scope', '$http', function($scope, $http) {
$scope.url = 'https://www.reddit.com/r/worldnews/new.json';
$scope.filter= 'test';
$scope.orderBy= 'sortExpression';
}])
.directive('myData', ['$http', function($http) {
return {
restrict: 'A',
scope: {
remoteurl: '=',
filter: '=',
orderBy: '='
// orderBy:'sortExpression':'order' ;
},
templateUrl: 'DataTable.html',
link: function(scope, element, attr) {
$http.get(scope.remoteurl)
.success(function(response) {
scope.names = response.data.children;
});
}
};
}]);
})();
数据表.html
<ul>
<li >
<table width="80%" id="dataTable" align="center" name="table1">
<tr>
<td><strong>Title</strong></td>
<td><strong>Link</strong></td>
<td><strong>Score</strong></td>
</tr>
<tr ng-repeat="x in names |filter:test|orderBy:sortExpression:order ">
<td id="title"><a ng-href="{{ x.data.url}}">{{x.data.title}}</a></td>
<td ><a ng-href="{{ x.data.url}}">Link</a></td>
<td>{{x.data.score}}</td>
</tr>
</table>
</li>
我正在传递所有参数,但只有 url 有效过滤器和 orderby 无效,有人可以纠正我吗?
【问题讨论】:
-
我猜你的 tr 应该是这样的:
<tr ng-repeat="x in names |filter:filter|orderBy:orderBy">。试试看。如果您可以准备 Plnkr,下次会更好,因此其他人可以直接在代码中进行调查。见http://plnkr.co -> 新 -> Angular 1.x -
@ilmgb 它的工作,但怎么能做 asc 和 desc?
-
参见https://docs.angularjs.org/api/ng/filter/orderBy 的角度文档。它声明“表达式可以可选地以 + 或 - 为前缀来控制升序或降序排序顺序(例如,+name 或 -name)。如果没有提供属性(例如 '+'),则使用数组元素本身比较排序位置。”
-
不要忘记将答案标记为正确。谢谢。
标签: angularjs filter directive