【问题标题】:Dragula-model not upating source arrayDragula-model 不更新源数组
【发布时间】:2016-05-13 06:49:16
【问题描述】:

我正在使用 angular-dragula 编写一个小测试代码,并且我已经设法让拖放操作正常工作,但它没有更新源数据数组。

我在角度控制器中声明了一些变量:

$scope.todos=['Stuff','Thangs'];
$scope.plan=new Array(($scope.endTime - $scope.startTime)*(60/$scope.timeBlock));

for(var i=0;i<$scope.plan.length;i++){
  $scope.plan[i]=["-"];
}

这可能不是最好的初始化方法,但我得到了一个数组数组,每个数组都包含一个“占位符”字符。

然后 HTML 将其绘制出来:

<div ng-app='dayplannerApp' ng-controller="MainCtrl">
  <div class='wrapper'>
    <button ng-click='dump()'>Dump</button>
  <div class='container' id='source-box' dragula='"first-bag"' dragula-model='todos'>
    <div id="source" ng-repeat='text in todos' ng-bind='text'></div>
  </div>
  <hr>
    <div class='container'><pre>{{plan[0] | json}}</pre></div>
    <div class='container'><pre>{{plan[1] | json}}</pre></div>
  <hr>
  <table ng-repeat="n in  Range()" border='1'>
    <tr ng-class-odd="'odd'" ng-class-even="'even'" ><td width='20%'>{{n}}</td><td>
      <div class='container' dragula='"first-bag"'  dragula-model='plan[$index]'>
      <div ng-attr-id="{{ 'block-' + n}}"  ng-repeat='text in plan[$index]'  ng-bind='text' ng-click='setId($event)'></div>
      </div></td></tr>
  </table>
</div>
  </div>

这似乎可行,我可以将“stuff”或“thangs”拖到包含 ng-repeat 的 div 中并可以将它们放在那里,如果我在前两个中放东西,两个“pre”容器会更新行。

但是当我尝试转储底层“计划”数组时:

console.log($scope.plan);

我只是得到了只有占位符的原始数组。我对模型分配做错了吗?还是绑定?为什么 plan[1] 在 div 中更新,但在 javascript 中没有?

【问题讨论】:

    标签: angularjs dragula


    【解决方案1】:

    我设法做的是重写:

    for (var hour = $scope.startTime; hour < $scope.endTime; hour++) {
           for (var mins = 0; mins < 60; mins+=$scope.timeBlock){
            var minVal = mins;
            if (mins===0){
              minVal='00';
            }
            result.push(
              {
                'Time': hour + ':' + minVal,
                'ToDo': []
              });
          }
        }
        return result;
    

    然后我用它作为来源:

    <table  border=1 style="table-layout:fixed;">
            <tr><th width='10%'>Time</th><th width='80%'>Activity</th><th width='5%'>Drop here</th></tr>
            <span>
            <tr ng-repeat="n in Day" ng-class-odd="'odd'" ng-class-even="'even'" >
                <td width='20%'>{{n.Time}}</td>
                <td width='80%'>{{n.ToDo[0]}}</td>
                <td width='10%' style='background-color:black;overflow:hidden' class='container' dragula='"first-bag"' dragula-model='n.ToDo'>
                  {{n.ToDo}}
                </td>
            </tr>
            </span>
          </table>
    

    不太确定问题出在哪里。是不是 plan[$index] 作为模型不会更新?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 2014-12-01
      • 2016-12-09
      • 2018-06-26
      • 2016-10-17
      相关资源
      最近更新 更多