AngularJS:drag-n-drop指令不起作用

时间:2015-11-01 17:07:19

标签: javascript angularjs drag-and-drop

我使用以下库:https://github.com/marceljuenemann/angular-drag-and-drop-lists

我试图将一个元素从一个列表移动(通过拖动)到另一个列表。 免责声明:我是新手,所以任何帮助都表示赞赏,我可能会做一些非常天真的错误。

这是我的代码:

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

app.controller( 'myCtrl', function ( $scope, $http, $log ) {
    $scope.lists = {serversList:[], selectedServersList:[]}
    $scope.lists.serversList =
    {
        label : "servers",
        allowedTypes : [
            'server'
        ],
        servers : [
            {
                name : "server1",
                type : "server"
            }, {
                name : "server2",
                type : "server"
            }, {
                name : "server",
                type : "server"
            }
        ]
    };
    $scope.lists.selectedServersList =
    {
        label : "selectedServers",
        allowedTypes : [
            'server'
        ],
        servers : []
    };

    // Model to JSON for demo purpose
    $scope.$watch('lists', function(lists) {
        $scope.modelAsJson = angular.toJson(lists, true);
    }, true);
};
/**
* For the correct positioning of the placeholder element, the dnd-list and
* it's children must have position: relative
*/
.servers-container ul[dnd-list],
.servers-container ul[dnd-list] > li {
    position: relative;
}

/**
* The dnd-list should always have a min-height,
* otherwise you can't drop to it once it's empty
*/
.servers-container ul[dnd-list] {
    min-height: 42px;
    padding-left: 0px;
}

/**
* The dndDraggingSource class will be applied to
* the source element of a drag operation. It makes
* sense to hide it to give the user the feeling
* that he's actually moving it.
*/
.servers-container ul[dnd-list] .dndDraggingSource {
    display: none;
}

/**
* An element with .dndPlaceholder class will be
* added to the dnd-list while the user is dragging
* over it.
*/
.servers-container ul[dnd-list] .dndPlaceholder {
    display: block;
    background-color: #ddd;
    min-height: 42px;
}

/**
* The dnd-lists's child elements currently MUST have
* position: relative. Otherwise we can not determine
* whether the mouse pointer is in the upper or lower
* half of the element we are dragging over. In other
* browsers we can use event.offsetY for this.
*/
.servers-container ul[dnd-list] li {
    background-color: #fff;
    border: 1px solid #ddd;
    border-top-right-radius: 4px;
    border-top-left-radius: 4px;
    display: block;
    padding: 10px 15px;
    margin-bottom: -1px;

    /* Disable text selection if item is not draggable */
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

/**
* Gender-specifc background
*/
.servers-container ul[dnd-list] li.background-man {
    background-color: #CAE0FC;
}

.servers-container ul[dnd-list] li.background-woman {
    background-color: #FFE2F5;
}

.servers-container ul[dnd-list] input.background-man {
    background-color: #D8E9FF;
    color: #2F4D99;
}

.servers-container ul[dnd-list] input.background-woman {
    background-color: #FFF0FA;
    color: #D84FA7;
}

/**
* Handle positioning
*/
.servers-container .handle {
    cursor: move;
    position: absolute;
    top: 14px;
}

.servers-container .name {
    margin-left: 20px;
}
<div ng-app='app'>
    <div class="row" ng-controller='myCtrl'>
        <div class="servers-container col-md-4">
            <ul dnd-list="lists.serversList"
                dnd-allowed-types="lists.serversList.allowedTypes">
                <li ng-repeat="server in lists.serversList.servers"
                    dnd-draggable="server"
                    dnd-type="server.type"
                    dnd-disable-if="server.type == 'unknown'"
                    dnd-moved="lists.serversList.servers.splice($index, 1)"
                    dnd-effect-allowed="move"
                    class="background-servers">
                    <div class="handle">:::</div>
                    <div class="name" dnd-nodrag>
                        <input type="text" ng-model="server.name" class="background-server form-control input-sm">
                    </div>
                </li>
                <li class="dndPlaceholder">Drop any <strong>server</strong> here</li>
            </ul>
        </div>

        <div class="servers-container col-md-4">
            <ul dnd-list="lists.selectedServersList"
                dnd-allowed-types="lists.selectedServersList.allowedTypes">
                <li ng-repeat="server in lists.selectedServersList.servers"
                    dnd-draggable="server"
                    dnd-type="server.type"
                    dnd-disable-if="server.type == 'unknown'"
                    dnd-moved="lists.selectedServersList.servers.splice($index, 1)"
                    dnd-effect-allowed="move"
                    class="background-servers">
                    <div class="handle">:::</div>
                    <div class="name" dnd-nodrag>
                        <input type="text" ng-model="server.name" class="background-server form-control input-sm">
                    </div>
                </li>
                <li class="dndPlaceholder">Drop any <strong>server</strong> here</li>
            </ul>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

我在调试控制台中运行了你的代码,发现没有定义splice上的调用。 经过进一步检查,我发现您的<ul dnd-list>元素实际上是基于您的serversList 对象,而不是您要迭代的实际事物列表。当我设置这个dnd列表时,我做了一些非常相似的事情。

serversList.servers数组实际上将成为您的UL的基础,然后您的li将成为该数组中的条目。这样,拼接功能实际上可以使用它。

这应该让你开始。我在一秒钟内开发激光标签,所以今晚我没有时间实施它;)。

到目前为止,我对这个插件非常满意,玩得开心!