Angular X-Editable - onbeforesave的附加参数

时间:2014-03-05 08:47:54

标签: angularjs x-editable

我使用angular-xeditable来内联编辑表值,并希望在输入模糊后将该值保存在数据库中。我想要做的是使用“onbeforesave”属性调用save函数。

问题是该函数仅将更改的值作为参数。我还需要应用xeditable的标记的id。

那么如何传递对DOM元素的引用(最佳选项)或仅传递元素id(在这种情况下就足够了)。

非常感谢!

2 个答案:

答案 0 :(得分:2)

在前端的ng-repeat上,在“onBeforeSave”属性上,您可以传递$index,这样就像onBeforesave="myFUnction($index)"一样,然后使用索引来定位数组元素改变价值观。因此,如果您的前端ng-repeat使用$scope.items,您将使用索引来定位$scope.items[index],然后您可以获取此索引上的所有数据并将数据传递到服务器以执行某些数据库更新

答案 1 :(得分:1)

请记住,您可以传递重复的对象,当您尝试从该索引中找出对象时,它可以提供帮助, 如下所述:

How to use Angular-Xeditable's onBeforeSave / onAfterSave methods with more than $data as parameter

<div ng-repeat="p in people">
    <a href="#" editable-text="p.name" onaftersave="updatePerson(p)">{{p.name}}</a>
</div>

然后,您的控制器中包含以下代码:

$scope.updatePerson = function(person) {
    var ret = PersonService.save(person); // or whatever save mechanism you use
    // You need to return a error string if save fails, true otherwise
    if (ret !== null) {  // You have to make sure this logic works for your save service
        return ret;
    }
    return true; 
}

我更喜欢这种解决方案,因为它避免了索引+过滤问题 如下所述: http://codeutopia.net/blog/2014/11/10/angularjs-best-practices-avoid-using-ng-repeats-index/