【发布时间】:2015-08-21 16:19:10
【问题描述】:
我正在使用 Angular 和 JSPlumb,我想将 jsplumb 中的可拖动行为绑定到指令中的元素(在链接函数中使用 element)。
目前我正在做这样的事情 (http://jsfiddle.net/r8epahbt/):
// In the controller I define a method to get the elements
// via jquery and then make them draggable
function MyCtrl($scope, $http, $log, $timeout) {
$scope.makeWidgetsDraggable = function() {
// ISSUE: I have to use jQuery here, how can I do it the Angular way?
// I only want to make the "current" element draggable (it's wasteful to do ALL .widgets)
// How can I make only THIS element (could be passed from directive below) draggable
jsPlumb.draggable($('#canvas .widget'), { // Do this $('#canvas .widget') - the Angular Way
containment: "parent"
});
};
}
// When the value of $scope.items changes, we call scope.makeWidgetDraggable
// which will get ALL widgets and make them draggable.
// I only want to make the newly created widget draggable
myApp.directive("widgetTemplate", function($parse, $timeout) {
//...
link: function (scope, element, attrs) {
// Watch the `items` for change, if so (item added)
// make the new element(s) draggable
scope.$watch('items', function() {
$timeout(function() {
// [ISSUE] - This method uses jQuery to get `this` element (and all other elements)
// How can I do this the `angular way` - I want to make `this` element draggable
// (the one that is being rendered by this directive)
scope.makeWidgetsDraggable();
// I want to do something like this:
// But it Gives error: TypeError: Cannot read property 'offsetLeft' of undefined
/*jsPlumb.draggable(element, {
containment: "parent"
});*/
}); // $timeout
}); // $watch
},// link
//...
}
我认为这样的事情应该可以工作(在指令中的链接函数中):
// Gives me error (through JSPlumb Library):
// TypeError: Cannot read property 'offsetLeft' of undefined
jsPlumb.draggable(element, {
containment: "parent"
});
我已经制作了一个有效的 JSFiddle,如果有人可以看一下,我将不胜感激。
所以基本上,我想找到一种更好的方法来执行第 11 行(在小提琴中)
// I want to remove this jquery selector
jsPlumb.draggable($('#canvas .widget'),...)
// and do it the `Angular Way`
jsPlumb.draggable(element, ...)
// Doesn't work, gives me error:
// TypeError: Cannot read property 'offsetLeft' of undefined
【问题讨论】: