在ng消息中动态设置表单名称
Dynamically set form name in ng-messages
在我的应用程序中的多个表单中,我试图使用相同的指令,这是一个电子邮件输入字段完成验证(它从最近使用的电子邮件列表中获取其值,但这与此问题无关)。因为使用该指令的形式取决于我使用它的地方,所以ng-messages属性需要动态呈现。
我已经设法让ng-messages动态设置自己,但它不像预期的那样工作。下面是代码:
form.html
<form name="joinForm">
<rt-recents-menu ng-model="vm.roomName"/>
</form>
directive.coffee
recents = angular.module 'rtRecents', ['ionic', 'ngMessages']
.factory('rtRecentsService', require './recentsService')
.directive('rtRecentsMenu', (rtRecentsService, $ionicActionSheet) ->
restrict: 'E'
template: require './recentsMenu.html'
scope:
ngModel: '=?'
require: ['?ngModel', '^form']
transclude: false
replace: false
link: (scope, element, attrs, ctrls) ->
ngModel = ctrls[0]
formCtrl = ctrls[1]
scope.formName = formCtrl.$name
module.exports = recents.name
directive.html
<div class="email-line">
<input type="email"
name="roomName"
required
ng-model="ngModel"
ng-keydown="onKeyDown()"/>
<div id="email-error-messages" ng-messages="{{formName}}.roomName.$error" multiple role="alert">
<div ng-message="required" class="warn" translate>VALIDATION.EMAIL_REQUIRED_ERROR</div>
<div ng-message="email" class="warn" translate>VALIDATION.EMAIL_INVALID_ERROR</div>
</div>
</div>
在我的例子中,我有一个显示从db查询返回的每一行的a。在这个重复循环中,每一行都有自己的形式。
我遇到了两个问题:
- ngMessage错误没有正确显示,因为它们似乎没有绑定到正确的字段/表单。 我无法禁用"保存"按钮。
这里是工作(修剪装饰等,以便您只看到要点):
<div ng-repeat="row in rows | orderBy: 'sortBy'"
ng-init="formName = 'siteEdit' + row.id">
<form name="{{formName}}" ng-submit="view.startSave(row.id)">
<!-- I have "control" buttons at the top of my form to save, cancel, etc. here are the two related to saving -->
<span class="glyphicon glyphicon-save pull-right q-palette-color-2" ng-disabled="view.isPageBusy()"
ng-show="! this[formName].$invalid" ng-click="view.startSave(row.id)" title="Save"/>
<span class="glyphicon glyphicon-ban-circle pull-right q-palette-accent-2"
ng-show="this[formName].$invalid" style="padding-left:10px;cursor: pointer; cursor: hand;" data-toggle="tooltip" title="Correct Errors To Save"/>
<label for="siteName{{row.id}}"> Name:</label>
<input id="siteName{{row.id}}"
class="form-control input"
type="text"
placeholder="name"
ng-disabled="view.isPageBusy()"
ng-model="row.name"
name="name"
required />
<div ng-messages="formName.name.$error">
<div ng-message="required">This field is required</div>
</div>
.....
</form>
</div>
重点:
- ng-init在ng-repeat中构建formName变量。$invalid
- 字段验证引用格式为formName..$error
注意事项:
我无法让ng-disable实际上禁用"按钮"基于它是无效的或不(ng-click执行无论什么,也许是因为我没有使用<button>
,但使用<span>
w/glyphicon
)。所以我选择让一个在你能保存时显示,另一个在你不能保存时显示。这样做效果更好,因为用户可以直观地看到他们不能保存,如果他们将鼠标悬停在上面,他们会看到他们需要先修改表单。
相关文章:
- 通过JavaScript设置表单目标
- 当按下链接以在不同页面上的表单中的下拉列表中设置值时
- 在提交时设置表单隐藏值
- 无法获取表单's面板的x和y坐标,并在加载时从cookie中设置它们
- 如何设置Dojo表单中的selected选项
- 如何从javascript中设置html表单的属性
- 如何在表单提交时在谷歌分析上设置事件跟踪
- 在表单提交时设置JSON格式
- Dojo表单.选择未正确设置值
- Extjs 3.3动态地将字段添加到表单中,并设置它们的标签宽度
- 需要使用条纹(更多)重力表单插件将默认付款表单设置为“存档卡”
- 如何在支柱中使用操作表单设置文本框值
- 本地存储表单以将最后一个当前表单设置为新的当前表单
- 将表单设置为只允许.edu电子邮件地址
- 如何在javascript中为放置在引导模式中的表单设置密码和用户名
- 如何根据对上一个表单的选择,将选择表单设置为选项
- 如何为Angular.js表单设置默认值和可变值
- 如何在不用户单击提交的情况下从表单设置JavaScript变量?
- Ajax表单设置文本框值onsubmit
- 将表单设置为$pure,但保持当前值