本文轉自: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;
}