【问题标题】:Ng-repeat and iScroll dynamic width not workingNg-repeat 和 iScroll 动态宽度不起作用
【发布时间】:2015-08-03 10:38:32
【问题描述】:

我有一个解决方案,它在另一个 ng-repeat 中包含一个 ng-repeat。我用它来创建两个包含静态内容和动态内容的表。在第一次加载滚动不起作用。使用内容调整页面大小后,滚动就可以正常工作。在 iPad 和 Android 平板电脑上,我遇到过滚动在第一次加载时有效,但宽度不包含表格中的最后两个静态 TD。

我很确定解决方案是为 iScroll 设置正确的 .refresh 方法,但我无法弄清楚正确的设置。

我曾尝试使用ng-iScroll 插件,但没有任何运气。我也尝试为 .refresh 方法设置超时,但仍然没有结果。

我能够用这个小提琴重建问题:jsfiddle.net/8BUjf/115/

当您运行小提琴时,滚动不起作用。稍微调整内容页面的大小后,滚动就可以在包含所有静态和动态 td 的正确宽度下正常工作。

下面的代码也可以在上面的小提琴中找到。

编辑

在 steppefox 给出答案后,我尝试使用指令设置解决方案。我现在在控制台中收到 0 个错误,并且角度正确显示了列表,但滚动仍然不起作用 - 即使我尝试调整内容的大小。

这是新的小提琴http://jsfiddle.net/8BUjf/149/,下面的代码也更新了。

我的 HTML

<div ng-app="app">
<div ng-controller="TodoCtrl">
<div class="tfl-overflow" id="iscrollwrapper" iscrollDirective>
<div id="iscroller">
            <table class="table" style="border-bottom: 1px solid #E77E23; ">
                <tr>
                    <td>
                        <h3 class="text-width">
                            <a href="">Static TD 1</a>
                        </h3>
                    </td>
                    <td ng-repeat="parentCategory in totalParentCategoriesCount" class="text-info">
                        <h3 class="text-width">
                            <a href="">Dynamic TD {{parentCategory.count}}</a>
                        </h3>
                    </td>
                    <td>
                        <div>
                            <h3 class="text-width">
                                <a href="">Static TD 2</a>
                            </h3>
                        </div>
                    </td>
                    <td>
                        <div>
                            <h3 class="text-width">
                                <a href="">Static TD 3</a>
                            </h3>
                        </div>
                    </td>
                </tr>
            </table>
            <table class="table table-striped">
                  <tr ng-repeat="fighter in totalFighterList">
                    <td>
                        <p class="text-width">Static TD</p>
                    </td>
                    <td ng-repeat="parentCategory in totalParentCategoriesCount">
                        <p class="text-width">Dynamic TD {{parentCategory.count}}</p>
                    </td>
                    <td>
                        <p class="text-width">Static TD 2</p>
                    </td>
                    <td>
                        <p class="text-width">Static TD 3</p></td>
                  </tr>
            </table>
        </div>
    </div>
</div>
</div>

角度

var mainApp = angular.module("app", []);

mainApp.directive('iscrollDirective', iscrollDirective);
iscrollDirective.$inject = ['$timeout'];
function iscrollDirective($timeout) {
return {
    restrict:'A',
    link: function ($scope, element, attrs) {
        $timeout(function(){
            var iscrollwrapper = new IScroll(element.attr('#iscrollwrapper'), {
                scrollX: true,
                scrollY: false,
                mouseWheel: false,
                scrollbars: false,
                useTransform: true,
                useTransition: false,
                eventPassthrough: true,
            });
            iscrollwrapper.refresh();
        })
    }
}
};

mainApp.controller('TodoCtrl', function($scope) {
$scope.totalParentCategoriesCount = [
    {count:1},
    {count:2},
    {count:3},
    {count:4},
    {count:5},
    {count:6}];

$scope.totalFighterList = [
    {count:1},
    {count:2},
    {count:3},
    {count:4},
    {count:5},
    {count:6}];
});

CSS

#iscroller{
display:inline-block;
width:auto;
}

.text-width{
width:150px;
}

.tfl-overflow {
overflow-x: auto !important;
-webkit-overflow-scrolling: touch;
}

#iscrollwrapper{
width:100%;
}

【问题讨论】:

  • 有人有什么建议吗?

标签: javascript angularjs iscroll


【解决方案1】:

问题是,因为 iScroll 的运行时间早于 ng-repeat 由 angular 运行的时间。

  1. 创建一个角度指令

    angular.module('app').directive('iscrollDirective', iscrollDirective); iscrollDirective.$inject = ['$timeout']; function iscrollDirective($timeout) { return { restrict:'A', link: function ($scope, element, attrs) { $timeout(function(){ iscrollwrapper = new IScroll(element.attr('id'), { scrollX: true, scrollY: false, mouseWheel: false, scrollbars: false, useTransform: true, useTransition: false, eventPassthrough: true, }); iscrollwrapper.refresh(); }) } } });

  2. 删除你在加载的函数中调用iscroll的JS代码,因为它现在没用了(我们有指令)。

  3. 添加到你的包装器&lt;div class="tfl-overflow" id="iscrollwrapper"&gt;属性iscroll-directive,就像&lt;div class="tfl-overflow" id="iscrollwrapper" iscroll-directive&gt;

2015-08-03 更新

http://jsfiddle.net/8BUjf/160 - 固定小提琴

【讨论】:

  • 谢谢你,但我无法让指令生效。我收到语法错误:SyntaxError: expected expression, got ')'。请查看更新的小提琴:jsfiddle.net/8BUjf/126/
  • 好的,所以我尝试使用我的解决方案配置此设置,但到目前为止,我认为您将 iscroll-directive 放入 html 并将 iscrollDirective 作为名称是错误的。所以我把它改成了一样的。 iscrollwrapper前面可能还缺少var?如果我改变这一切,我不会得到任何错误,角度工作,但滚动不会 - 即使我调整窗口大小。请看这个小提琴:http://jsfiddle.net/8BUjf/149/
  • jsfiddle.net/8BUjf/160 - 我已经稍微更新了你的代码。属性 iscroll-directive - 必须是这样,它是 Angular 指令的规则,指令的名称必须是驼峰式,但属性带有破折号。此外,问题出在new IScroll(element.attr('#iscrollwrapper')),因为它错误地调用了属性值。请检查我的饲料,我希望它会有所帮助。
  • 我刚刚阅读了 angular 的属性和指令。我现在看到了我的错误。它完美地工作!谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多