本文转自:https://www.zybuluo.com/dreamapplehappy/note/54448
多步表单的实现
在线demo演示地址https://rawgit.com/dreamapplehappy/AngularJS-uiRouter/master/index.html
文件的构成
说明:先新建一个文件夹(例如:router),然后新建下面的文件。
- index.html
- form.html
- form-required.html
- form-optional.html
- form-confirm.html
- myApp.js
- myStyle.css
详解每个文件的代码
- index.html 代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>多步表单</title>
<link rel="stylesheet" href="lib/bootstrap.min.css">
<link rel="stylesheet" href="myStyle.css">
<script src="lib/angular.js"></script>
<script src="lib/angular-ui-router.js"></script>
<script src="lib/angular-animate.js"></script>
<script src="myApp.js"></script>
</head>
<body ng-app="myModule">
<div class="container" ng-controller="myFormController">
<div ui-view></div>
</div>
</body>
</html>
代码说明:
ng-app="myModule" 表示AngularJS启动的开始
ng-controller="myFormController" 表示这整个div由控制器myFormController来控制
新建一个lib文件夹,放在router目录下
外部引入的angular.js,angular-ui-router.js,angular-animate.js,bootstrap.min.css都放在这个文件夹中
这个demo的路由配置放在myApp.js里面
demo的样式文件放在myStyle.css里面
<div ui-view></div>这个div包含ui-view说明里面放置的是html模板
- form.html
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<div class="form-container">
<div>
<h2>欢迎注册Dreamapple!</h2>
<div class="control">
<a ui-sref-active="active" ui-sref=".required"><span>1</span> 基本信息</a>
<a ui-sref-active="active" ui-sref=".optional"><span>2</span> 选填项</a>
<a ui-sref-active="active" ui-sref=".confirm"><span>3</span> 确认结果</a>
</div>
<hr />
</div>
<form ng-submit="submit()">
<div class="form-view" ui-view></div>
</form>
</div>
<hr />
<pre>
{{ formData }}
</pre>
</div>
</div>
代码说明:
ui-sref=".required"说明当点击这个链接的时候路由会跳转到相应的包含required状态的路由中。
ng-submit="submit()"说明表单提交的时候运行的函数
- myApp.js 最重要的一部分
//定义自己的module(myModule)
//中括号中的是这个module的依赖
var myModule = angular.module("myModule", ['ngAnimate','ui.router']);
myModule.config(['$stateProvider','$urlRouterProvider',function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('form',{
url:'/form',
templateUrl:'form.html',
controller:'myFormController' //指明控制器
})
.state('form.required',{
url:'/required',
templateUrl:'form-required.html'
})
.state('form.optional',{
url:'/optional',
templateUrl:'form-optional.html'
})
.state('form.confirm',{
url:'/confirm',
templateUrl:'form-confirm.html'
});
$urlRouterProvider.otherwise('/form/required'); //匹配所有不在上面的路由
}]);
//定义模块的控制器
myModule.controller('myFormController', ['$scope', function($scope){
$scope.formData = {};
$scope.submit = function() {
alert("Cool, you have registered!");
};
}]);
代码说明都包含在注释中
- form-required.html
<div class="form-group">
<label for="username">用户名:</label>
<input type="text" class="form-control" name="username" ng-model="formData.username">
</div>
<div class="form-group">
<label for="password">密码:</label>
<input type="password" class="form-control" name="password" ng-model="formData.password">
</div>
<div class="form-group">
<label for="cp">确认密码:</label>
<input type="password" class="form-control" name="cp" ng-model="formData.cp">
</div>
<div class="form-group">
<label for="email">邮箱:</label>
<input type="email" class="form-control" name="email" ng-model="formData.email">
</div>
<div class="form-group">
<button ui-sref-active="active" ui-sref="form.optional" class="btn btn-success">下一项</button>
</div>
- form-optional.html
<div class="form-group">
<label for="gender">您的性别:</label>
<input type="radio" value="man" class="form-control" ng-model="formData.gender">男
<input type="radio" value="woman" class="form-control" ng-model="formData.gender">女
</div>
<br />
<div class="form-group">
<button ui-sref-active="active" ui-sref="form.required" class="btn btn-success">上一项</button>
<button ui-sref-active="active" ui-sref="form.confirm" class="btn btn-success">下一项</button>
</div>
- form-confirm.html
<div class="form-group">
<label for="interest">选择你喜欢的语言:</label>
<input type="checkbox" value=".net" class="form-control" ng-model="formData.interestA">.NET
<input type="checkbox" value="php" class="form-control" ng-model="formData.interestB">PHP
<input type="checkbox" value="nodejs" class="form-control" ng-model="formData.interestC">NodeJS
<hr />
<button type="submit" class="btn btn-primary">注册</button>
</div>
- myStyle.css
@keyframes slideToRight {
from { transform:translateX(100%); }
to { transform: translateX(0); }
}
@-moz-keyframes slideToRight {
from { -moz-transform:translateX(100%); }
to { -moz-transform: translateX(0); }
}
@-webkit-keyframes slideToRight {
from { -webkit-transform:translateX(100%); }
to { -webkit-transform: translateX(0); }
}
.form-view.ng-enter{
position: absolute;
transition:0.5s all linear;
-moz-transition:0.5s all linear;
-webkit-transition:0.5s all linear;
}
.form-view.ng-enter{
-webkit-animation:slideToRight 0.5s both linear;
-moz-animation:slideToRight 0.5s both linear;
animation:slideToRight 0.5s both linear;
}
div.control a.active span{
background-color: #666;
color: #FFF;
}
div.control a{
text-decoration: none;
display: inline-block;
width: 20%;
}
div.control a span{
display: inline-block;
width: 36px;
height: 36px;
border-radius: 36px;
text-align: center;
line-height: 36px;
}
.form-container{
height: 399px;
}
.textarea{
width: 100% !important;
height: 60px !important;
}