【问题标题】:Why is <paginator> not binding to $scope?为什么 <paginator> 不绑定到 $scope?
【发布时间】:2015-08-13 09:03:22
【问题描述】:

示例

所以下面的模板被加载为模态的内容。我可以阅读我设置的范围,但我无法理解我没有将&lt;pagination&gt; 绑定到totalItems

模板.html

<div class="modal-header">
    <h3 class="modal-title">{{$parent.helpName}}</h3>
</div>
<div class="modal-body">
    Page {{currentPage }} of {{totalItems}}
    <pagination boundary-links="true" total-items="totalItems" ng-model="currentPage" class="pagination-sm" previous-text="&lsaquo;" next-text="&rsaquo;" first-text="&laquo;" last-text="&raquo;"></pagination>
    <div data-ng-show="page.pageNumber == currentPage" data-ng-repeat="page in $parent.helpPages">
        <ul tabset typeof="pills">
            <li tab data-ng-repeat="subPage in page.pills" data-heading="{{subPage.tabTitle}}">
                <div class="panel panel-default">
                    <div class="panel-body" style="overflow:auto;" data-ng-bind-html="subPage.pillDiv">
                    </div>
                </div>
            </li>
        </ul>
    </div>
</div>

Controller.js

app.directive('myDirective', function factory (){
    return {
        restrict:   'A',    // IE8 doesn't support 'custom tags'
        template:'<a ng-hide="!overlayName" class="btn btn-primary" ng-click="showHelp(overlayName)">Need Help <i class="fa fa-question-circle"></i></a>',
        controller: function overlayCtrl ($scope, $modal, data) {
            // [A]  overlay control properties
            $scope.helpModal = null;
            $scope.overlayName = __hasOverlay($scope.$parent.step.shortDesc); // Right now, look up the overlay by the shortDesc
            if ($scope.overlayName) $scope.overlayName = $scope.overlayName.help;

            // [B]  overlay control methods
            $scope.showHelp = function (name) {

                data.getHelpOverlay(name).then(function (helpContent) {
                    $scope.helpPages = helpContent;
                    $scope.helpName = name;
                    $scope.helpModal = $modal.open({
                        scope:$scope, controller:function ($scope, $sce) {
                            // the scope I'd expect <pagination>'s model to bound on
                            $scope.currentPage = 1;
                            $scope.totalItems = $scope.$parent.helpPages.length;
                        // other code...
                        },
                        templateUrl:'app/steps/templates/helpOverlay.html',
                        size:'lg'
                    });
                });
            }
        }
    };
});

什么给了?!

【问题讨论】:

  • 你要把my-directive属性放在html的什么地方?
  • @pankajparkar - 它嵌套在更高级别的模板中
  • @jme11 - 该对象是什么样的?我没有看到angular-ui.github.io/bootstrap/#/pagination$scope.totalItems 设置为对象的示例;这是一个数字。
  • @jme11 我在另一个线程stackoverflow.com/questions/18642371/… 上找到了这个 - 将尝试使用页面和计数的孤立对象。
  • 等等,看起来你正在将范围传递给模态范围,所以 $scope.totalItems = $scope.helpPages.length;应该工作。

标签: javascript angularjs pagination angular-ui-bootstrap


【解决方案1】:

编辑

我认为您误解了 total-items 是要在分页控件中显示的总页数。 total-items 是 ALL 页面中的项目总数。 (我想我应该更仔细地查看您的问题,但问题不在于您无法访问 totalItems,而是分页在您期望它显示 4 时仅显示一页)。

items-per-page 的默认值为 10。因此,如果您只传入 4 个项目,它只会显示 1 页。

Plunker

<pagination boundary-links="true" total-items="totalItems" ng-model="currentPage" class="pagination-sm" previous-text="&lsaquo;" next-text="&rsaquo;" first-text="&laquo;" last-text="&raquo;" items-per-page="1"></pagination>

【讨论】:

  • 感谢您的努力;我也很喜欢我的特定版本的 anuglar 和 ui-bootstrap,它成功地将 &lt;pagination&gt; 绑定到模态模板中。我接受了您的建议并清理了内部控制器。还是没有骰子!
  • 和之前的结果一样。
  • 它是页面数组的长度,查看顶部的图片,totalItems 值暴露在范围内,并在角度表达式中按预期进行计算。
  • 我发现了你的问题,它与你的指令无关。这就是您设置分页控件的方式。您需要设置 items-per-page = 1。将修复答案。
  • 你喝啤酒,享受你的星期六吧!
猜你喜欢
  • 2014-09-18
  • 1970-01-01
  • 2016-11-13
  • 2021-04-24
  • 2014-05-10
  • 1970-01-01
  • 2020-07-03
  • 1970-01-01
  • 2011-12-04
相关资源
最近更新 更多