How to make template without ng-repeat and pass data to $scope with angular-drag-and-drop-lists?

925 views Asked by At

I want to use angular-drag-and-drop-lists with my own grid template to WYSIWYG editor. How to build my own HTML template without ng-repeat so it will be able to drop items in any predefined column and store information in $scope in which column item has been dropped?

I want to store dropped items in columns array:

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

        ],
        "B": [

        ]
      }
    ]
  };
}
]);

This is currently not a working template. On drop it throws the error "undefined is not an object (evaluating '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>

This is the standard working aproach:

<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>

Based on this example: demo

1

There are 1 answers

0
Paul Sweatte On

How to build my own HTML template without ng-repeat

Use $templateCache and a for loop as an alternative:

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>

References