1.form.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello AngularJS</title>
<meta name="keywords" content="HelloWorld" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0" name="viewport">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<script src="js/common/angular.min.js"></script>
<script src="js/common/angular-ui-router.min.js"></script>
</head>
<body ng-app="notesApp" ng-controller="MainCtrl as ctrl">
<form ng-submit="ctrl.submit()">
<input type="text" ng-model="ctrl.user.username">
<input type="password" ng-model="ctrl.user.password">
<input type="submit" value="submit">
</form>
</body>
<script type="text/javascript">
angular.module('notesApp',[])
.controller('MainCtrl', [function(){
var self = this;
self.submit = function() {
console.log('submit with ', self.user);
}
}]);
</script>
</html>
2.页面展示
3. 解析
- 使用ng-submit比ng-click好处是,回车他也可以提交。
- 前端ctrl.user.username和ctrl.user.password在输入的时候自动生成一个user对象进行封装。