请告诉我我的代码中有什么问题。单击表标题时,$ scope.sort将获取正确的值,但不会按字母顺序对表进行排序。
您可以在此处使用 JSON 文件查看plunkr:http://plnkr.co/edit/DYwbqDzBVTkQalhDtc6i?p=preview
在此处查看CONSOLE LOG http://embed.plnkr.co/DYwbqDzBVTkQalhDtc6i/
var ssgaApp = angular.module('ssgaApp', []);
ssgaApp.controller('mainCtrl',['$scope', '$http', function ($scope,$http) {
var getEntries = $http.get('altLoginServlet.json');
getEntries.success(function (data, status, headers, config) {
$scope.ajaxerror = false;
$scope.companies = data.data;
});
getEntries.error(function(data, status, headers, config) {
$scope.ajaxerror = true;
});
$scope.sort = {column: '', descending: true};
$scope.changeSort = function(column) {
// console.log($scope.sort);
$scope.sort.column = column;
$scope.sort.descending = !$scope.sort.descending;
console.log('$scope.sort.column', $scope.sort.column, '$scope.sort.descending', $scope.sort.descending);
$scope.currentPage = 1;
$scope.currentPage_grid = 1;
};
}])
.directive('changeSortdirective', ['$timeout', function($timeout) {
return {
restrict: 'A',
scope: {
changeSortAttr: '@changeSortdirective'
},
link: function (scope, element, attrs) {
console.log('changeSortAttr', scope.changeSortAttr);
element.on('click', function(){
console.log('changeSort bind', scope.changeSortAttr);
scope.$parent.changeSort(scope.changeSortAttr);
});
}
};
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="ssgaApp" ng-controller="mainCtrl">
<table class="leftPadding">
<thead>
<tr>
<th width="80%">
<a href="javascript:void(0)" change-sortdirective="companyName">Name <span></span></a>
</th>
<th>
<a href="javascript:void(0)" change-sortdirective="id">ID <span></span></a>
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="comp in companies | orderBy:sort.column:sort.descending ">
<td class="col1" ng-show="comp.companyName"><a href="javascript:void(0)" ng-click='toggleModal(comp.id)'>{{comp.companyName}}</a></td>
<td class="col1" ng-show="comp.loginName">
<a ng-click="doAltLogin(comp.loginName)" href="javascript:void(0)">{{comp.lastName}}, {{comp.firstName}}</a></td>
<td ng-bind="comp.id"></td>
</tr>
</tbody>
</table>
</div>
答案 0 :(得分:2)
由于您使用的是DOM onclick
事件而非ngClick
指令,因此Angular无法检测到用户的互动。
您可能需要手动刷新$scope
:
element.on('click', function() {
scope.$apply(function() {
scope.$parent.changeSort(scope.changeSortAttr);
});
});
答案 1 :(得分:1)
Very simple use
$scope.$apply(function() {
$scope.sort=angular.copy($scope.sort);
});
Plunker="http://plnkr.co/edit/f5lAp33c8VSwnVwDVLhA?p=preview"