5

我的任务是使用 angular 处理删除的文本。$scope is not defined我在拖放事件中不断收到此错误。知道如何解决这个问题吗?

我已经研究了角度拖放库。他们不允许拖放简单的文本。他们中的大多数都使用列表。让我知道是否有适合此任务的

这是plunker:

[ http://plnkr.co/edit/egKL13hsHka6RiX4UfSS?p=preview][1]

这是控制器:

var app = angular.module("test",  []);
app.controller('testCtrl', ['$scope',function  ($scope) {
   $scope.nouns = ['guitar'];
   $scope.verbs = ['play'];
   $scope.allowDrop= function (ev) {
        ev.preventDefault();
   };
   $scope.drag=  function (ev) {
        ev.dataTransfer.setData("Text", ev.target.id);
   };    
   $scope.drop=  function (ev) {
        ev.preventDefault();
        var data = ev.dataTransfer.getData("Text");
        if(ev.target.id =='verb' && $scope.verbs.indexOf(data) != -1){
            ev.target.appendChild(document.getElementById(data));
        }
        else{
            alert(data + ' is not a ' + ev.target.id +'. Try again');
        }            
    };      
  }]);
4

2 回答 2

6

$scope object will not available in "Outside angular context". Whenever you want to access angular scope in JavaScript (Outside angular world) then you need to get the scope of by getting the DOM of that element & then access the scope of it like angular.element(document.getElementById('testApp')) which is nothing but scope of the body.

Working Plunkr

Refer this SO answers to get access to the scope outside angular context.

于 2015-05-12T21:02:22.353 回答
1

基于@pankaj-parkar 答案的较短的一个:

<div draggable="true" ondrag="angular.element(this).scope().on_drag(event)"></div>
于 2017-03-08T06:39:41.537 回答