javascript 具有可编辑元素的表单应具有“editable-form”属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24996429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Form with editable elements should have `editable-form` attribute
提问by Alex Man
I have a form within which i have used x-editable
plugin for editing a text-field. But i am getting the following script error. Can anyone please tell me some solution for this
我有一个表单,我在其中使用了x-editable
插件来编辑文本字段。但我收到以下脚本错误。谁能告诉我一些解决方案
Form with editable elements should have `editable-form` attribute. <span editable-text="user.name" e-form="textBtnForm" class="ng-scope ng-binding editable">
html
html
<div ng-app='myApp' ng-controller="ArrayController">
<form action="#" >
<span editable-text="user.name" e-form="textBtnForm">
{{ user.name || 'empty' }}
</span>
<button class="btn btn-default" ng-click="textBtnForm.$show()" ng-hide="textBtnForm.$visible">edit
</button>
</form>
</div>
script
脚本
var app = angular.module('myApp', ["xeditable"]);
app.controller('ArrayController', function ($scope) {
$scope.test = "manu";
$scope.user = {
name: 'awesome user'
};
});
回答by runTarm
There are a couple things to do:
有几件事要做:
- add the
editable-form
attribute to the form as the error suggest. - remove the
e-form="textBtnForm"
, it's not required for your requirement. - instead, set the
textBtnForm
as a name of the form. - add
type="button"
to the edit button, otherwise it doesn't work (don't know why ..). - I've also add save and cancel button when editing for the sake of completeness.
editable-form
按照错误提示将属性添加到表单中。- 删除
e-form="textBtnForm"
,它不是您的要求所必需的。 - 相反,将 设置
textBtnForm
为表单的名称。 - 添加
type="button"
到编辑按钮,否则它不起作用(不知道为什么..)。 - 为了完整起见,我还在编辑时添加了保存和取消按钮。
The result would look like this:
结果如下所示:
<form editable-form name="textBtnForm" action="#">
<span editable-text="user.name">
{{ user.name || 'empty' }}
</span>
<button type="button" class="btn btn-default" ng-click="textBtnForm.$show()" ng-hide="textBtnForm.$visible">
edit
</button>
<span ng-show="textBtnForm.$visible">
<button type="submit" class="btn btn-primary" ng-disabled="textBtnForm.$waiting">
Save
</button>
<button type="button" class="btn btn-default" ng-disabled="textBtnForm.$waiting" ng-click="textBtnForm.$cancel()">
Cancel
</button>
</span>
</form>
JSFiddle:http://jsfiddle.net/5TZX5/1/
JSFiddle:http : //jsfiddle.net/5TZX5/1/
Hope this helps.
希望这可以帮助。
回答by David Bohunek
回答by Victor Orletchi
the previous approach is partial correct and can be applied only if use a single input in that form. Also if you try to add more elements, then will not work correct. So the right solution that i have is to use ng-form with editable-form directive as block for each form element (input) that you want apply the xedit plugin, and of course remove editable-form from main form.
前面的方法是部分正确的,只有在使用该形式的单个输入时才能应用。此外,如果您尝试添加更多元素,则将无法正常工作。因此,我拥有的正确解决方案是使用带有 editable-form 指令的 ng-form 作为您想要应用 xedit 插件的每个表单元素(输入)的块,当然还从主表单中删除 editable-form。
the sample based by your code is below:
基于您的代码的示例如下:
<div ng-app='myApp' ng-controller="ArrayController">
<form action="#" >
<div ng-form editable-form name="textBtnForm">
<span editable-text="user.name" ng-click="textBtnForm.$show()">{{user.name||'empty'}}</span>
<span ng-show="textBtnForm.$visible">
<button type="button" class="btn btn-primary" ng-disabled="textBtnForm.$waiting" ng-click="textBtnForm.$submit()"> Save</button>
<button type="button" class="btn btn-default" ng-disabled="textBtnForm.$waiting" ng-click="textBtnForm.$cancel()">cancel</button>
</span>
</div>
<hr>second form element<hr>
<div ng-form editable-form name="textBtnForm2">
<span editable-text="user.phone" ng-click="textBtnForm2.$show()">{{ user.phone || 'empty' }}</span>
<span ng-show="textBtnForm2.$visible">
<button type="button" ng-disabled="textBtnForm2.$waiting" ng-click="textBtnForm2.$submit()">Save</button>
<button type="button" class="btn btn-default" ng-disabled="textBtnForm2.$waiting" ng-click="textBtnForm2.$cancel()"> Cancel</button>
</span>
</div>
</form>
</div>