可排序 – 将参数从指令传递到回调
发布时间:2020-12-17 07:35:02 所属栏目:安全 来源:网络整理
导读:我试图定义一个包装 jqueryui可排序插件的指令排序. 角度代码是: module.directive('sortable',function () { return function (scope,element,attrs) { var startIndex,endIndex; $(element).sortable({ start:function (event,ui) { startIndex = ui.item
我试图定义一个包装
jqueryui可排序插件的指令排序.
角度代码是: module.directive('sortable',function () { return function (scope,element,attrs) { var startIndex,endIndex; $(element).sortable({ start:function (event,ui) { startIndex = ui.item.index(); },stop:function (event,ui) { endIndex = ui.item.index(); if(attrs.onStop) { scope.$apply(attrs.onStop,startIndex,endIndex); } } }).disableSelection(); }; }); html代码是: <div ng-controller="MyCtrl"> <ol sortable onStop="updateOrders()"> <li ng-repeat="m in messages">{{m}}</li> </ol> </div> MyCtrl的代码: function MyCtrl($scope) { $scope.updateOrders = function(startIndex,endIndex) { console.log(startIndex + "," + endIndex); } } 我想在我的回调updateOrders中获取startIndex和endIndex,并与它们进行一些操作,但它打印: undefined,undefined 如何将这些参数传递给我的回调?我的方法是否正确?
范围.$apply接受函数或字符串.
在这种情况下,使用函数会更简单: scope.$apply(function(self) { self[attrs.onStop](startIndex,endIndex); }); 不要忘记将您的html代码更改为: <ol sortable onStop="updateOrders"> (已删除()) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |