淺談angular表單提交中ng-submit的默認使用方法
在表單提交的時候,我使用了一個button,但ng-submit寫在form標簽中,然而button中我未使用任何方法訪問submit()函數(shù)
<div ng-app="dkr"> <div ng-controller="logincontrol"> <form ng-submit="submit(user)"> <div>賬號名 <input type="text" ng-model="user.username"/></div> <div>密碼 <input type="text" ng-model="user.password"></div> <button type="submit">提交</button> <div ng-show="success.length>0">{{success}}</div> <div ng-show="error.length>0">{{error}}</div> </form> </div>
<code class="language-html"></div></code>
angular.module("dkr",[]) .controller("logincontrol",function($scope){ $scope.user={username:'',password:''}; $scope.success=""; $scope.error=""; $scope.submit=function(u){ if($scope.user.username=="admin"&&$scope.user.password=="123"){ $scope.success="success"; $scope.error=""; } else{ $scope.success=""; $scope.error="error"; } console.log(u); }; })
點擊提交后的結果:
如上所示,返回結果以及控制臺打印完全正確。
然而我對提交這個button未做任何處理
然后經(jīng)過測試,我發(fā)現(xiàn)button的默認type為submit。
因為只要將
<button>提交</button>
改成
<button type="button">提交</button>
之后
點擊按鈕沒有任何反應。
因此在form表單提交的時候一定要注意button標簽的默認使用。
以上這篇淺談angular表單提交中ng-submit的默認使用方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
AngularJS框架中的雙向數(shù)據(jù)綁定機制詳解【減少需要重復的開發(fā)代碼量】
這篇文章主要介紹了AngularJS框架中的雙向數(shù)據(jù)綁定機制,結合實例形式分析了AngularJS雙向數(shù)據(jù)綁定機制的原理及實現(xiàn)方法,以及減少需要重復開發(fā)代碼量的優(yōu)勢,需要的朋友可以參考下2017-01-01使用AngularJS和PHP的Laravel實現(xiàn)單頁評論的方法
這篇文章主要介紹了使用AngularJS和PHP的Laravel實現(xiàn)單頁評論的方法,本文的示例是前端JavaScript和后端PHP聯(lián)合編程的典范,需要的朋友可以參考下2015-06-06AngularJS ng-repeat數(shù)組有重復值的解決方法
不知道大家是否遇到過這個問題,在當Angular.JS ng-repeat數(shù)組中有重復項時,系統(tǒng)就會拋出異常,這是該怎么做?本文通過示例代碼介紹了詳細的解決方法,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-10-10