您可以使用一个指令(这是更多的AngularJS方式),或者因为你必须包含jQuery for uiKit,你可以使用jQuery,就像你的问题一样。
无论使用哪种方法,如果您想更新范围属性,则需要在$scope.apply()
函数中包装。
指令版本:
var app = angular.module('App', []);
app.controller('AppCtrl', function($scope) {
$scope.movedItemDirectiveVersion = "No item has been moved yet...";
});
app.directive("mySortable", function() {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
elem.on('change.uk.sortable.mySortable', function(event, sortableObj, element, action) {
scope.$apply(function() {
scope.movedItemDirectiveVersion = "You moved " + element[0].innerText;
});
});
// clean up when scope is destroyed
scope.$on("$destroy",
function() {
elem.off("change.uk.sortable.mySortable");
}
);
}
}
});
<ul class="uk-sortable uk-grid uk-grid-small uk-grid-width-1-5" data-uk-sortable="" my-sortable>
<li class="uk-grid-margin" style="">
<div class="uk-panel uk-panel-box">Item 1</div>
</li>
<li class="uk-grid-margin" style="">
<div class="uk-panel uk-panel-box">Item 2</div>
</li>
</ul>
非指导版本:
var app = angular.module('App', []);
app.controller('AppCtrl', function($scope) {
$scope.movedItem = "No item has been moved yet...";
$('#withoutDirective').on("change.uk.sortable", function(event, sortableObj, element, action) {
$scope.$apply(function() {
$scope.movedItem = "You moved " + element[0].innerText;
});
});
});
<ul id="withoutDirective" class="uk-sortable uk-grid uk-grid-small uk-grid-width-1-5" data-uk-sortable="">
<li class="uk-grid-margin" style="">
<div class="uk-panel uk-panel-box">Item 1</div>
</li>
<li class="uk-grid-margin" style="">
<div class="uk-panel uk-panel-box">Item 2</div>
</li>
</ul>
演示了两个版本:http://jsfiddle.net/s9fr43of/1/
也可以不使用jQuery:'的document.getElementById( '样本')的addEventListener( 'change.uk.sortable',函数(){...})' –
@IvanMinakov:我想使用AngularJS。 – Stuart