javascript - Angular Js Drag and Drop - Passing $index to Directive -


i have been successful in implementing drag , drop feature in angular js.

the basic idea match city countries. able drag city , drop in country box. but, dropped item gets reflected in country box. since have used ng-repeat finding hard fetch $index value in directive.

my implementation here:

var module = angular.module('my-app', []);    module.directive('draggable', function () {      return {          restrict: 'a',          link: function (scope, element, attrs) {              element[0].addeventlistener('dragstart', scope.handledragstart, false);              element[0].addeventlistener('dragend', scope.handledragend, false);          }      }  });    module.directive('droppable', function () {      return {          restrict: 'a',          link: function (scope, element, attrs) {              element[0].addeventlistener('drop', scope.handledrop, false);              element[0].addeventlistener('dragover', scope.handledragover, false);          }      }  });    module.controller("maincontroller", function ($scope) {      $scope.questions = [{          city: "delhi",          country: "india"      }, {          city: "tokyo",          country: "japan"      }, {          city: "doha",          country: "qatar"      }, ];        $scope.answers = [];        $scope.handledragstart = function (e) {          this.style.opacity = '0.9';          e.datatransfer.setdata('text/plain', this.innerhtml);      };        $scope.handledragend = function (e) {          this.style.opacity = '1.0';      };        $scope.handledrop = function (e) {          e.preventdefault();          e.stoppropagation();          var datatext = e.datatransfer.getdata('text/plain');          $scope.$apply(function () {              $scope.answers[id].country = $scope.questions[$index].country              $scope.answers[id].city = datatext;          });          console.log($scope.answers[$index]);      };        $scope.handledragover = function (e) {          e.preventdefault();          e.datatransfer.dropeffect = 'move';          return false;      };  });
.container {      width: 100%;      border: 1px solid #ccc;      box-shadow: 0 1px 5px #ccc;      border-radius: 5px;      font-family: verdana;      margin: 25px auto;  }  .left {      float: left;  }  .right {      float : right;  }  .container header {        padding: 10px;  }  .container h1 {      padding: 0;      margin: 0;      font-size: 16px;      font-weight: normal;      text-shadow: 0 1px 2px white;      color: #888;      text-align: center;  }  .container section {      padding: 10px 30px;      font-size: 12px;      line-height: 300%;      color: #333;  }  .default {      clear : both;  }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>  <body ng-app="my-app">      <div ng-controller="maincontroller">           <h3>match following</h3>            <div class="container">              <header>                   <h1>drag , drop city corresponding country box</h1>                </header>              <section>                  <div draggable="true" ng-repeat="qs in questions">{{qs.city}}</div>              </section>          </div>          <div class="container" ng-repeat="qs in questions">              <header>                   <h1>{{qs.country}}</h1>                </header>              <section droppable="true"> <span>{{droppedcity}}</span>               </section>          </div>          <div class="default"> <pre>{{items|json}}</pre>          </div>      </div>  </body>

any useful. thanks.

there quite few problems code. i'd suggest studying official documentation , questions related custom directives , isolate scopes. here updated code many changes , updated fiddle:

<body ng-app="my-app"> <div ng-controller="maincontroller">      <h3>match following</h3>      <div class="container">         <header>              <h1>drag , drop city corresponding country box</h1>          </header>         <section>             <div draggable="true" ng-repeat="qs in questions" index="{{$index}}">{{qs.city}}</div>         </section>     </div>     <div class="container" ng-repeat="qs in questions">         <header>              <h1>{{qs.country}}</h1>          </header>         <section droppable="true" index="{{$index}}"> <span>{{droppedcity}}</span>          </section>     </div>     <div class="default"> <pre>{{items|json}}</pre>     </div> </div> 

var module = angular.module('my-app', []);  module.directive('draggable', function () {     return {         restrict: 'a',         scope: {             index: '@'         },          link: function (scope, element, attrs) {             element[0].addeventlistener('dragstart', scope.handledragstart, false);             element[0].addeventlistener('dragend', scope.handledragend, false);         },         controller: function($scope) {             $scope.handledragstart = function (e) {                 console.log('starting drag', $scope.index);                 this.style.opacity = '0.9';                 e.datatransfer.setdata('text/plain', this.innerhtml);             };              $scope.handledragend = function (e) {                 this.style.opacity = '1.0';             };         }     }; });  module.directive('droppable', function () {     return {         restrict: 'a',          scope: {             index: '@'         },         link: function (scope, element, attrs) {             element[0].addeventlistener('drop', scope.handledrop, false);             element[0].addeventlistener('dragover', scope.handledragover, false);         },         controller: function($scope) {             $scope.handledrop = function (e) {                 e.preventdefault();                 e.stoppropagation();                 var datatext = e.datatransfer.getdata('text/plain');                 $scope.$parent.answers.push({country: $scope.$parent.questions[$scope.index].country});                 $scope.$parent.answers.push({city: $scope.$parent.questions[$scope.index].city});                                 };              $scope.handledragover = function (e) {                 e.preventdefault();                 e.datatransfer.dropeffect = 'move';                 console.log('dragging over', $scope.$parent.questions[$scope.index].country);                 return;             };         }     }; });  module.controller("maincontroller", function ($scope) {     $scope.questions = [{         city: "delhi",         country: "india"     }, {         city: "tokyo",         country: "japan"     }, {         city: "doha",         country: "qatar"     }, ];      $scope.answers = []; }); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -