Bài 17: Làm thế nào đế sử dụng ng-submit trong Angularjs

Lượt xem: 4192

Xem demo

Bài 17: Làm thế nào đế sử dụng ng-submit trong Angularjs

Để trả lời cho câu hỏi ”Làm thế nào để sử dụng ng-submit trong Angularjs” chúng ta cùng tìm hiểu ví dụ sau:


 <html>  
   <head>  
    <title>Sử dụng ng-submit trong Angularjs </title>  
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>  
     <script>  
       angular.module('MyForm', [])  
           .controller('myController', ['$scope', function($scope) {  
 $scope.login = function () {  
             if ($scope.username == 'admin' && $scope.password == '123456') {  
               alert(‘Đăng nhập thành công.’);  
             }  
             else {  
               alert('Đăng nhập không thành công.');  
             }  
           };  
            }]);  
     </script>  
   </head>  
   <body ng-app="MyForm" name = "myform" ng-submit="login()" >  
    Tên đăng nhập: <br/>
	     <input type="text" name="userName" ng-pattern="/^([a-zA-Z0-9]+)$/" ng-model="username" ng-trim="true" required ng-minlength="3" ng-maxlength="10" />    <br/>
     Mật khẩu: <br/>
    <input type="text" name="userPass" ng-model="password" required ng-minlength="3" ng-maxlength="10" ng-trim="true">    <br/><br/>
     <input type="submit" value='Đăng nhập' />  
  
   </body>  
 </html> 

Xem demo