2015-06-21 17 views
0

調用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。

有什麼辦法?謝謝

回答

2

好吧,我發現我可以得到表單的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') 
    } 
]