以下是我的Pagination Directive
angular.module('frontendApp')
.directive('postsPagination', function() {
return{
restrict: 'E',
template: '<ul class="pagination">'+
'<li ng-show="currentPage != 1"><a href="javascript:void(0)" ng-click="getList(1)">«</a></li>'+
'<li ng-show="currentPage != 1"><a href="javascript:void(0)" ng-click="getList(currentPage-1)">‹ Prev</a></li>'+
'<li ng-repeat="i in range" ng-class="{active : currentPage == i}">'+
'<a href="javascript:void(0)" ng-click="getList(i)">{{i}}</a>'+
'</li>'+
'<li ng-show="currentPage != totalPages"><a href="javascript:void(0)" ng-click="getList(currentPage+1)">Next ›</a></li>'+
'<li ng-show="currentPage != totalPages"><a href="javascript:void(0)" ng-click="getList(totalPages)">»</a></li>'+
'</ul>'
};
});
我希望ng-click
中的函数名称是动态的
我:e ng-click = getList
答案 0 :(得分:0)
看看这个:https://jsfiddle.net/n9KNv/14/
使用$ compiler进行角度解释ng-click in指令。喜欢这个
angular.module('frontendApp')
.directive('postsPagination', function(){
return{
restrict: 'E',
template: $compile('<ul class="pagination">'+
'<li ng-show="currentPage != 1"><a href="javascript:void(0)" ng-click="getList(1)">«</a></li>'+
'<li ng-show="currentPage != 1"><a href="javascript:void(0)" ng-click="getList(currentPage-1)">‹ Prev</a></li>'+
'<li ng-repeat="i in range" ng-class="{active : currentPage == i}">'+
'<a href="javascript:void(0)" ng-click="getList(i)">{{i}}</a>'+
'</li>'+
'<li ng-show="currentPage != totalPages"><a href="javascript:void(0)" ng-click="getList(currentPage+1)">Next ›</a></li>'+
'<li ng-show="currentPage != totalPages"><a href="javascript:void(0)" ng-click="getList(totalPages)">»</a></li>'+
'</ul>');
};
});
答案 1 :(得分:0)
您可以使用javascript eval()
方法。
eval函数可以动态执行JavaScript源代码。
要将您的函数名称字符串转换回javascript表达式,可以使用eval()
方法,如下所示:
var dateFn = "Date(1971,3,8)";
var myDate;
eval("myDate = new " + dateFn + ";");
document.write(myDate);
// Output: Thu Apr 8 00:00:00 PDT 1971
答案 2 :(得分:0)
directive.html
<ul class="pagination">
<li ng-show="currentPage != 1">
<a href="javascript:void(0)" ng-click="getList(1)">«</a></li>
<li ng-show="currentPage != 1">
<a href="javascript:void(0)" ng-click="getList(currentPage-1)">‹ Prev</a></li>
<li ng-repeat="i in range" ng-class="{active : currentPage == i}">
<a href="javascript:void(0)" ng-click="getList(i)">{{i}}</a></li>
<li ng-show="currentPage != totalPages">
<a href="javascript:void(0)" ng-click="getList(currentPage1)">Next ›</a></li>
<li ng-show="currentPage != totalPages">
<a href="javascript:void(0)" ng-click="getList(totalPages)">»</a></li>
controller1.js
angular
.module('frontendApp')
.controller('Ctrl1', function(){
$scope.getList = function(){} // your implementation 1 here
});
controller2.js
angular
.module('frontendApp')
.controller('Ctrl2', function(){
$scope.getList = function(){} // your other implementation here
});
现在在两个地方都使用相同的html文件,只需更改控制器名称,ypu就可以为同一个函数设置不同的实现,从而重用模板:
angular.module('frontendApp')
.directive('postsPagination', function() {
return{
restrict: 'E',
templateUrl: 'directive.html',
controller: 'Ctrl1'
};
});
angular.module('frontendApp')
.directive('someOther', function() {
return{
restrict: 'E',
templateUrl: 'directive.html',
controller: 'Ctrl2'
};
});