簡體   English   中英

使用Server驗證后,從angularjs提交表單

[英]Submit Form from angularjs after validate with Server

我有html表單和角度控制器。 在控制器中,我有一個功能來檢查服務器上的用戶數據。 我通過使用html中的按鈕調用此函數

ng-click

成功后,我想要從控制器提交表格。

如何從角度控制器訪問form.submit

謝謝 !!!

我建議你在表單上使用ng-submit指令。 從那里你可以在驗證后返回true / false。

調節器

app.controller('mainController', function($scope) {
  $scope.submit = function(user) {
    var isvalid = true;

    // Add your code 
    // for server validation here

    if (isvalid) {
        return true;
    }
    return false; //failed
  }
});

HTML

<form name="userForm" ng-submit="submit(user)">
    <input type="text" ng-model="user.email" />
    <input type="text" ng-model="user.password" />
    <button type="submit">Submit</button>
</form>

假設你ng-click

validate();

在這個函數中你可以做如下的驗證和表單提交操作(我的語法可能不正確......但我只想傳達我的想法)

$scope.vaidate = function(input) { 

    //Add your validation code here, it includes api call etc.

    // If validation succeeds, run code for submitting the form. 
    if() // add validation success condition
    {
         //run code for submitting the form.
    }
    else
    {
         // Add code to show error message within the html form.
    }

}

我認為您可以在if條件下運行提交操作。

我已經使用jquery通過方法提交選擇表單來修復它

$("#myForm").submit();

它適合我!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM