我对角度很新,而且我很难将头部缠绕在物品被推到的地方。我不确定我是否正确设置要与拖放一起使用的函数,以及它是否绑定到较旧的范围对象并且ng-repeat没有正确更新。我认为我的设置方式存在一些细微问题。任何指针或帮助将非常感激。
当您将可拖动容器中的颜色拖到Droppable容器中时,它应该更新链接到范围对象项的文本。我成功地将一个项目推送到范围对象上,但ng-repeat没有将其拾取。我不确定我是否需要手表或做些什么才能让它注意新添加的物品。
JS小提琴:http://jsfiddle.net/RV23R/
HTML CODE:
<div ng-app="my-app" ng-controller="MainController">
<div class="container">
<header><h1>Draggables</h1></header>
<section>
<div draggable="true" ng-repeat="drag_type in drag_types">{{drag_type.name}}</div>
</section>
</div>
<div class="container">
<header><h1>Drop Schtuff Here</h1></header>
<section droppable="true">
<div><span>You dragged in: </span><span ng-repeat="items in items">{{item.name}},</span></div>
</section>
</div>
角色代码:
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);
}
}
});
function MainController($scope)
{
$scope.drag_types = [
{name: "Blue"},
{name: "Red"},
{name: "Green"},
];
$scope.items = [];
$scope.handleDragStart = function(e){
this.style.opacity = '0.4';
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.items.push(dataText);
console.log($scope.items);
};
$scope.handleDragOver = function (e) {
e.preventDefault(); // Necessary. Allows us to drop.
e.dataTransfer.dropEffect = 'move'; // See the section on the DataTransfer object.
return false;
};
}
CSS(如果有人关心)
.container {
width: 600px;
border: 1px solid #CCC;
box-shadow: 0 1px 5px #CCC;
border-radius: 5px;
font-family: verdana;
margin: 25px auto;
}
.container header {
background: #f1f1f1;
background-image: -webkit-linear-gradient( top, #f1f1f1, #CCC );
background-image: -ms-linear-gradient( top, #f1f1f1, #CCC );
background-image: -moz-linear-gradient( top, #f1f1f1, #CCC );
background-image: -o-linear-gradient( top, #f1f1f1, #CCC );
box-shadow: 0 1px 2px #888;
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: 175%;
color: #333;
}
答案 0 :(得分:8)
小提琴中有一些拼写错误,但基本问题是你的拖拽事件超出角度消化周期。您应该将更改包装在$scope.$apply
中(代码示例即将发布)。这个分叉和错误固定的(FIDDLE)表示当您单击按钮时,angular显示更改并使用新值刷新显示。
修复:(FIDDLE)
$scope.$apply(function() {
$scope.items.push(dataText);
});
您遇到的错误在于此代码:
<span ng-repeat="items in items">{{item.name}},</span>
这应该是ng-repeat="item in items"
,项目只包含删除的文本,因此它是一个字符串数组而不是原始项目对象。