Angular Xeditable - 无法使用 onbeforesave 事件获取正确的验证值

Angular Xeditable - can't get correct value for validation using onbeforesave event

我正在尝试将验证逻辑添加到我们使用的消息格式值的编辑中。我创建了 plunker here 来显示这个问题。这是花括号验证的代码。

vm.formatCheck = function (resource) {

console.log(resource.Value);

if (resource.Value.indexOf('{') > -1 || resource.Value.indexOf('}') > -1) {

    var x = resource.Value.split('{').length - 1;
    var y = resource.Value.split('}').length - 1;

    if ((x + y) % 2 === 1) {
        alert("Incorrect Message format");
        return;
    }

  }
};

示例:如果您编辑第一个值 ({JobRole} for {Organisation}) 并删除结尾的大括号 ({JobRole} for {Organisation)

alert("Incorrect Message format"); 永远不会显示,因为它得到的是原始值 - {JobRole} for {Organisation} 而不是 {JobRole} for {Organisation

如果我将验证逻辑移动到 onaftersave 事件,我会得到正确的值并触发验证,但它会 show/save 不正确的值,而我 不会想要。那么我该如何解决这个问题呢?任何帮助表示赞赏。

这是我的 plunker,它正在工作:Plunker

基本上,我将你的 html 更改为:

<a href="#" e-name="resource" editable-textarea="res.Value" e-rows="5" e-cols="30"
                   onbeforesave="vm.formatCheck($data)"
                   onaftersave="vm.onGridItemChanged(res)">{{ res.Value || 'empty' }}</a></a>

我传递的是 $data 而不是 'res'。 $data 是您实际更改的对象 属性 的值。

在控制器中,您的验证函数。 'Resource' 现在是 $data,其中包含您要更改的 属性 的值。所以你检查它,你 return 一条错误消息。如果您不想在字段旁边显示错误消息 return "";

vm.formatCheck = function (resource) {

        console.log(resource);

        if (resource.indexOf('{') > -1 || resource.indexOf('}') > -1) {

            var x = resource.split('{').length - 1;
            var y = resource.split('}').length - 1;

            if ((x + y) % 2 === 1) {
                alert("Incorrect Message format");
                return "Incorrect Message format";
            }

        }
    };