使用angularjs将HTML表单数据从页面提交到另一个html页面

时间:2016-04-14 05:39:51

标签: javascript html angularjs

我需要使用angularjs从HTML到HTML页面的简单表单提交。 我尝试使用localstorage广播服务注入器等,但没有工作请帮助。

我的pluker代码在这里  http://plnkr.co/edit/q3siKVPe9Nt0vXysIYVD?p=preview

HTML CODE:

<form name="personForm1" action="form2.html" method="post" novalidate>
  <label for="firstNameEdit1">First name:</label>
  <input id="firstNameEdit1" type="text" name="firstName" ng-model="firstName" required /><br />
  <label for="lastNameEdit1">Last name:</label>
  <input id="lastNameEdit1" type="text" name="lastName" ng-model="lastName" required /><br />
  <br />
  <button type="submit">Submit</button>
</form>

JS CODE:

angular.module("mainModule", [])
 .controller("mainController", function($scope, $http) {
   $scope.firstname = {};
   $scope.lastname = {};


   $scope.submitData = function(person, resultVarName) {
     var config = {
       params: {
         person: person
     }
   };

   $http.post("Form2.html", null, config)
     .success(function(data, status, headers, config) {
       $scope[resultVarName] = data;
     })
     .error(function(data, status, headers, config) {
       $scope[resultVarName] = "SUBMIT ERROR";
     });
   };
 });

1 个答案:

答案 0 :(得分:0)

Angular js是spa框架,这意味着只有一个主html页面可以是应用程序的home/default页面,其他页面如登录/忘记密码将存储为{{1}并在此主页内呈现。

您可以将此过程视为templates逻辑,无论何时需要呈现任何页面,都会换出旧页面(模板)并呈现新页面(模板)。

请参阅此plunker演示,以更好地了解此过程。

以下是来自plunker的一些重要事项

路由代码:

swap-in swap-out

导航到新模板的代码,附加参数(如果有的话)

app.config(['$routeProvider', function($routeProvider) { $routeProvider .when('/home', { templateUrl: 'template.html', controller: 'Ctrl' }) // route for the welcome/dashboard page .when('/welcome/:userName', { templateUrl: 'welcome.html', controller: 'welcomeCtrl' }) // route for the forgot/Recover password page .when('/forgotPass', { templateUrl: 'forgotPass.html', controller: 'forgotPassCtrl' }) //fallback url if nothing matches .otherwise({ redirectTo: '/home' }) }]);

水疗中心有许多优点:

  • 在大多数页面上出现的一些常见组件可以放在main中,只有特定的html快照在主页面上交换或换出,这样就不会重复同一段代码。
  • 渲染时间得到改善,因为每次页面导航时都不会渲染页眉/页脚等常见组件。
  • 帮助实现页眉/页脚等常见组件的关注分离,从而提高代码的模块性和可读性。
相关问题