我正在尝试在我的网络应用程序中创建一个多步骤表单以进行注册。 我正在使用 ui.router 来实现这一点。
当用户首先访问我的域时,例如 www.mydomain.com。我的node.js 服务器呈现index.ejs 页面。
app.get('/',function(req, res) {
res.render('index.ejs'); // load the index.ejs file
});
现在要知道的重要一点是 index.ejs 也是我在 ui-view
中注入(inject) View 的地方:
<!doctype html>
<html ng-app="myApp">
<head>
<title>Node Authentication</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pure/0.6.0/pure-min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0-beta.5/angular-animate.min.js"></script>
<link href="./css/side-menu.css" rel="stylesheet" type="text/css">
<script src="./js/mainApp.js"></script>
<script src="./js/controllers/knowledgeBaseCtrl.js"></script>
<script src="./js/services/knowledgeBaseFilter.js"></script>
</head>
<body ng-controller="projectsFilterCtrl">
<div class="container">
<div>
<h2>Our multistep form</h2>
<div id="status-buttons" class="text-center">
<a ui-sref-active="active" ui-sref="signup.stepOne"><span>1</span> Step One</a>
<a ui-sref-active="active" ui-sref="signup.stepTwo"><span>1</span> Step Two</a>
<a ui-sref-active="active" ui-sref="login"><span>2</span> login </a>
</div>
</div>
// INJECT VIEW HERE
<div ui-view></div>
</div>
</script>
</body>
</html>
我的 AngularJs Controller :
var knowledgeBaseCtrl = angular.module('knowledgeBaseCtrl', ['ngAnimate','ui.router'])
knowledgeBaseCtrl.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('login', {// this will be the wrapper for our wizard
url: '/login',
templateUrl: './login.ejs',
})
.state('signup', {// this will be the wrapper for our wizard
url: '/signup',
templateUrl: './index.ejs',
})
.state('signup.stepOne', {// this will be the wrapper for our wizard
url: '/stepOne',
templateUrl: './stepOne.ejs',
})
.state('signup.stepTwo', {// this will be the wrapper for our wizard
url: '/stepTwo',
templateUrl: './stepTwo.ejs',
});
// catch all route
// send users to the form page
$urlRouterProvider.otherwise('login');
});
现在的问题:
现在,当我访问 www.mydomain.com/#/signup/stepOne
时,它会加载内容两次,如下图所示。
我认为这是因为我的node.js服务器加载index.ejs,然后我的ui.router再次加载index.ejs,因此复制了页面。
我该如何解决这个问题?
最佳答案
不要将您的 index
文件放在路由中 - 您设置为模板的任何内容都将完整注入(inject) ui-view
中,因此您可以有效地嵌套 View 中应用程序的第二个实例。您的 index.ejs
应该只包含应用中每个部分都相同的内容。
您应该创建一个 signup.ejs
模板,其中仅包含应在注册页面上显示的内容,并在其中添加另一个您想要 subview 的 ui-view
待注入(inject)。
关于javascript - 使用 ui.router AngularJs 进行路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35336496/