【发布时间】:2015-06-04 22:27:19
【问题描述】:
我在 angularjs 中有一个多步骤表单。我正在使用 ui-router 在不同视图之间进行路由,并使用 ngResource 将数据发送到 mongodb。我的问题是我无法将表单的所有值直接发送到数据库中,因为表单被拆分为多个视图。当我使用 ng-submit 提交表单时,只有该视图的值被发送到数据库。
index.html
<body>
<div ui-view></div>
</body>
多个视图(register.html)
<div class="row">
<div>
<div id="form-container">
<div class="page-header">
<h2 class="text-center">Enter the following details</h2>
<!-- the links to our nested states using relative paths -->
<!-- add the active class if the state matches our ui-sref -->
<div id="status-buttons" class="text-center link-colors">
<a ui-sref-active="active" ui-sref="/registration_form.signUp"><span>1</span> Setup</a>
<a ui-sref-active="active" ui-sref="/registration_form.personalDetails" ng-disabled="myForm.email.$invalid"><span>2</span> Personal Details</a>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<form name="myForm" role="form" class="form-horizontal" id="signup-form" ng-submit="createMeetup()" enctype="multipart/form-data" novalidate>
<!-- our nested state views will be injected here -->
<div class="form-elements" id="form-views" ui-view></div>
</form>
</div>
<div class="container">
<form name="myForm" role="form" class="form-horizontal" id="signup-form" ng-submit="createMeetup()" enctype="multipart/form-data" novalidate>
<!-- our nested state views will be injected here -->
<div class="form-elements" id="form-views" ui-view></div>
</form>
</div>
在这个视图中会有两个表单视图
signUP.html
<label >Email</label>
<div >
<input type="email" name="email" ng-model="employeeEmail" placeholder="email" class="form-control" required>
<span ng-show="myForm.email.$error.email">Invalid Email.</span>
</div>
<a ui-sref="/registration_form.personalDetails">next section</a>
personalDetails.html
<label>First Name</label>
<div >
<input type="text" ng-model="first_name" class="form-control">
</div>
<button type="submit" name="submit">save</button>
我编写了以下控制器:app.js
var app = angular.module('meetupApp', ['ngResource','ui.router']);
app.controller('mainCrl', ['$scope', '$resource', '$location', function ($scope, $resource, $location) {
var Employee = $resource('/api/meetups');
$scope.employees = [];
$scope.nextPage = function(){
$scope.email = $scope.employeeEmail;
}
$scope.createMeetup = function () {
var employee = new Employee();
employee.email = $scope.employeeEmail;
employee.first_name = $scope.first_name;
employee.$save(function (result) {
});
}
}]);
app.config(function($stateProvider, $urlRouterProvider){
$stateProvider
.state('/',{
url:'/',
templateUrl:'index.html',
controller: 'mainCrl'
})
.state('/registration_form',{
url:'/registration_form',
templateUrl: 'register.html',
controller: 'mainCrl'
})
.state('/registration_form.signUp',{
url:'/registration_form.signUp',
templateUrl:'signUp.html',
controller:'mainCrl'
})
.state('/registration_form.personalDetails',{
url:'/registration_form.personalDetails',
templateUrl:'personalDetails.html',
controller:'mainCrl'
})
$urlRouterProvider.otherwise('/');
});
所以每当我单击提交按钮时,只有personalDetails.html 视图上的值会保存在数据库中...。signUp.html 视图的值在路由时会丢失。我该如何解决这个问题。 抱歉问了这么长的问题。
【问题讨论】:
标签: javascript angularjs node.js mongodb