【问题标题】:ui-sortable acting strange with ng-modelui-sortable 在 ng-model 中表现得很奇怪
【发布时间】:2018-06-26 12:50:11
【问题描述】:

在我的 AngularJS 应用程序中,我创建了一个带有列表的 ui-sortable。当我尝试以不同的方式订购列表时,它表现得很糟糕。没有ng-model我没有这个问题,但是ui-sortable需要有ng-model,否则会在控制台打印错误。为什么 ng-model 会产生这个问题?我创建的模型对象有误吗?

我在thisjsFiddle 中重现了错误。

重现:将 Laurent 拖到列表底部,Laurent 将出现在中间,而不是应该出现在底部。

我希望有人可以帮助我解决这个问题。

【问题讨论】:

    标签: angularjs angular-ui-sortable


    【解决方案1】:

    这似乎是 ui-sortable 中的一个错误,您正在使用这两个库的一个非常过时的版本。 查看源代码
    https://github.com/angular-ui/ui-sortable/blob/v0.0.1/src/sortable.js 并将其与您的版本进行比较 https://cdnjs.cloudflare.com/ajax/libs/angular-ui/0.4.0/angular-ui.js 经过一些调试后,您可能会注意到有

    onStop = function(e, ui) {
                // digest all prepared changes
                if (ui.item.sortable.resort && !ui.item.sortable.relocate) {
    
                  // Fetch saved and current position of dropped element
                  var end, start;
                  start = ui.item.sortable.index;
                  end = ui.item.index();
                  if (start < end)
                    end--;
    
                  // Reorder array and apply change to scope
                  ui.item.sortable.resort.$modelValue.splice(end, 0, ui.item.sortable.resort.$modelValue.splice(start, 1)[0]);
    
                }
                if (ui.item.sortable.resort || ui.item.sortable.relocate) {
                  scope.$apply();
                }
              };
    

    删除 if 语句 start

     onStop = function(e, ui) {
                // digest all prepared changes
                if (ui.item.sortable.resort && !ui.item.sortable.relocate) {
    
                  // Fetch saved and current position of dropped element
                  var end, start;
                  start = ui.item.sortable.index;
                  end = ui.item.index();
    
                  // Reorder array and apply change to scope
                  ui.item.sortable.resort.$modelValue.splice(end, 0, ui.item.sortable.resort.$modelValue.splice(start, 1)[0]);
    
                }
                if (ui.item.sortable.resort || ui.item.sortable.relocate) {
                  scope.$apply();
                }
              };
    

    我不建议手动更改库代码,而是升级到新版本的代码,可能会有更多功能和更多错误修复。

    【讨论】:

    • 我在本地更新了sortable.js文件到latest release,但是问题依然存在。它的反应和以前一样..
    • 根据 ui-sortable 的要求更新 angular、jquery 和 jquery-ui 并尝试 agian。
    • 给我一个新的小提琴。你的例子非常简单。如果它不工作,那就意味着 ui-sortable 坏了,它不是在演示站点上工作的。
    猜你喜欢
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 2014-12-12
    • 2012-11-11
    • 1970-01-01
    相关资源
    最近更新 更多