我正在尝试编写一个指令,它将在我的应用程序的任意位置对任意数据进行排序。让我们说我有以下代码(根据实际真正的代码,排序函数和一些用于简化复杂的)包装为指令的通用功能
angular.module('test', []);
angular.module('test').controller('wrapperController',['$scope', function(scope){
scope.data = {}
scope.data.rows = [{name: 'foo'}, {name: 'bar'}, {name: 'bazz'}]
scope.data.columns = [{name: 'Name', sortBy: 'name'}]
}]);
angular.module('test').directive('grid', [function(){
return {
restrict: 'A',
templateUrl: 'grid.html',
scope: {
grid: '='
}
}
}]);
angular.module('test').directive('sortable', [function(){
return {
restrict: 'A',
scope: {
sortableCollection: '=',
sortableKey: '&'
},
compile: function(el, at, transclude){
if(at['ng-click']){
el.attr('ng-click', el.attr('ng-click')+';sortFunction()')
}else{
el.attr('ng-click', 'sortFunction();')
}
return(function(scope, element, attrs){
scope.sortableKey = scope.sortableKey();
scope.sortFunction = function(){
alert(" I AM IN UR FUCNTION SORTING UR D00dZ!!1");
}
});
}
}
}]);
而且下面的HTML:
<body ng-app='test'>
<div ng-controller='wrapperController'>
<div grid='data'></grid>
</div>
</body>
和(在grid.html
):
<div>
<table>
<thead>
<tr>
<td ng-repeat='column in grid.columns'>
<div sortable sortable-collection='grid' sortable-key='column.sortBy'>{{column.name}}</div>
</td>
</tr>
</thead>
<tbody>
<tr ng-repeat='row in grid.rows'>
<td ng-repeat='cell in grid.columns'>
{{row.name}}
</td>
</tr>
</tbody>
</table>
</div>
检查的HTML显示,ng-click
正确填充,但是当标题被点击的功能永远不会触发。这是为什么?有没有办法获得这种行为?
plunr:http://plnkr.co/edit/aXjMqhZxI7ME8wQJOLpA?p=preview
+1,完美。当然绑定点击。我在ng-click上挂了太多。 –