如何在没有ng-repeat的情况下制作模板并使用angular-drag-and-drop-lists将数据传递到$ scope?

时间:2014-12-17 14:30:12

标签: javascript angularjs angularjs-scope angular-dragdrop

我想将angular-drag-and-drop-lists与我自己的网格模板一起用于WYSIWYG编辑器。如何在没有ng-repeat的情况下构建自己的HTML模板,以便它能够删除任何预定义列中的项目并将信息存储在已删除列项的$scope中?

我想将已删除的项目存储在columns数组中:

.controller('DragDropGrid', ['$scope',
function($scope) {
  $scope.layouts = {
    selected: null,
    templates: [
    {name: "Plugin", type: "item", id: 2},
    ],
    columns: [],
    oldaproachcolumns: [
        "A": [

        ],
        "B": [

        ]
      }
    ]
  };
}
]);

目前这不是一个有效的模板。在drop下它会抛出错误" undefined不是一个对象(评估' targetArray.splice')":

<div ng-include="'grid.html'"></div>
<script type="text/ng-template" id="grid.html">
  <div class="col-md-12 dropzone box box-yellow">
    <div class="row template-grid" dnd-list="list">
      <div class="col-xs-12 col-md-8" ng-repeat="item in list" dnd-draggable="item" ng-include="item.type + '.html'">Header</div>
      <div class="col-xs-6 col-md-4">News</div>
    </div>
  </div>
</script>

<script type="text/ng-template" id="item.html">
  <div class="item">Plugin {{item.id}}</div>
</script>

这是标准的工作方法:

<div class="row">
  <div ng-repeat="(zone, list) in layouts.oldaproachcolumns" class="col-md-3">
    <div class="dropzone box box-yellow">
      <h3>{{zone}}</h3>
      <div ng-include="'list.html'"></div>
    </div>
  </div>
</div>

<script type="text/ng-template" id="list.html">
<ul dnd-list="list">
<li ng-repeat="item in list" dnd-draggable="item" dnd-effect-allowed="move" dnd-moved="list.splice($index, 1)" dnd-selected="layouts.selected = item" ng-class="{selected: layouts.selected === item}" ng-include="item.type + '.html'">
</li>
</ul>
</script>

基于此示例:demo

1 个答案:

答案 0 :(得分:0)

  

如何在没有ng-repeat的情况下构建自己的HTML模板

使用$templateCachefor循环替代:

var app = angular.module('foo', []);


function bop(model, data)
  {
  return data ? model : 'foo';
  }

function baz()
  {
  return bop;
  }

function foo($templateCache)
  {
  var i = 0, len = 5, bar = "";
  
  for (i; i < len; i++)
  	{
    bar = bar.concat("<li>",i,"<p ng-include=\u0022'foo'\u0022></p></li>");
    }
  
  $templateCache.put('listContent', bar);
  }

angular.module('foo').filter('baz', baz);
app.run(foo);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="foo">
  <script type="text/ng-template" id="foo">
    <span>hi</span>
  </script>
  <input ng-model="dude">
  <ul ng-include="'listContent' | baz:dude"></ul>
</div>

<强>参考