如何在角度中创建自定义指令

时间:2017-03-24 08:50:10

标签: javascript angularjs pagination

我正在尝试一个示例分页,因为我已经创建了这样的指令(我不确定它是否正确)

(function() {
app.directive("myPagination", function() {
    return {
        restrict: 'E',
        templateUrl: 'pag.html'
    };
 });
})();

我的pag.html

<ul class="pagination" ng-controller="PagingController">
<li ng-repeat="x  in pageing" ng-click="change(x.pageno)"><a href="#">{{x.pageno}}</a></li>
</ul>

我的PageingController就像这样

 app.controller('PagingController', function($scope) {
    $scope.$on('pageinfo', function(event, args) {
        $scope.numbtn = args.numbtn;
        $scope.totaldata = args.totaldata;
        $scope.selet = args.selet;
        $scope.starter();
    })

    $scope.starter = function() {
        $scope.pageing = [];
        let i;
        for (i = 0; i < $scope.numbtn; i++) {
            $scope.pageing[i] = i;
        }
        console.log($scope.pageing);
    }
    $scope.change = function(btnclk) {
        alert(btnclk);
    }
});

<my-pagination></my-pagination>这就是我在索引页面中尝试的方式。但问题是它没有显示任何错误,任何人都可以纠正这个错误

1 个答案:

答案 0 :(得分:2)

SELECT EquipmentSerials.SerialNo,EquipmentSerials.IDNo,Equipments.Name,Equipments.TechOrder,Equipments.WorkUnitCode,Equipments.NationalStockNumber,Equipments.Manufacturer,Equipments.PartNumber,EquipmentSerials.ID,EquipmentSerials.EquipmentID,Discrepancy.Symbol FROM EquipmentSerials INNER JOIN Equipments ON (EquipmentSerials.EquipmentID = Equipments.ID) INNER JOIN Discrepancy ON (EquipmentSerials.ID = Discrepancy.EquipmentSerialsID) WHERE Discrepancy.Symbol='-' 控制器添加到指令而不是模板

PageingController

Demo

确保订阅者事件(function() { app.directive("myPagination", function() { return { restrict: 'E', templateUrl: 'pag.html', controller : 'PagingController' }; }); })() 正在执行。否则页面数组为空