从指令
问题描述:
调用ngSubmit你好我有一个指令,改变一个按钮的点击标签。我只想从指令中调用ngSubmit进行表单验证。可能吗?从指令
指令
angular.module 'myapp'
.directive 'nextButton', [() ->
return {
restrict: 'A'
replace: true
transclude: false
scope: {
saveLocal: '&saveDataLocal'
nextTab: '='
}
link: (scope, elem, attrs) ->
console.log scope.saveLocal
elem.bind 'click',()->
console.log attrs
scope.saveLocal 'app'
angular.element('#'+scope.nextTab).trigger('click')
}
]
我的html在玉模板:
form#generalFormScc(name="form" ng-submit="saveForm(form, $event)")
input(type='text', placeholder='Name', ng-model="name", required)
button(next-button next-tab="'next'" save-data-local="ngSubmit" form-name="'generalFormScc'") Next
这里, 下一个标签是触发下一个标签的ID, 保存数据本地应触发ngSubmit
我知道我可以直接调用saveForm()from指令,但我想要默认的html验证。所以我想触发ngSubmit。
有什么办法?谢谢
答
好吧,我发现我可以得到表单的id,并呼吁其提交方法以供将来参考。
'use strict'
angular.module 'myApp'
.directive 'nextButton', [() ->
return {
restrict: 'A'
replace: true
transclude: false
scope: {
saveLocal: '&saveDataLocal'
nextTab: '='
formName : '='
}
link: (scope, elem, attrs) ->
console.log scope.saveLocal
elem.bind 'click',()->
angular.element('#'+scope.formName).submit()
if not angular.element('#'+scope.formName).hasClass 'ng-invalid'
angular.element('#'+scope.nextTab).trigger('click')
}
]