AngularJS 1.6.8:表单数据未提交,因此无法将其保存到localStorage

时间:2018-02-01 08:32:58

标签: javascript angularjs forms angularjs-1.6 angularjs-forms

我有联系表格。提交时,它会显示成功消息,并应将该数据存储到$localStorage

但是,表格数据并非提交,因为我在开发工具的网络下看不到提交的表单数据,因此我无法将其保存到$localStorage

下面是各个文件的代码。 link to plunker contact.html

<div ngController="contactController as vm">
        <div class="heading text-center">
        <h1>Contact Us</h1>
    </div>
    <div>
        <form class="needs-validation" id="contactForm" novalidate method="post" name="vm.contactForm" ng-submit="saveform()">
            <div class="form-group row">
                <label for="validationTooltip01" class="col-sm-2 col-form-label">Name</label>
                <div class="input-group">
                    <input type="text" class="form-control" id="validationTooltipName" placeholder="Name" ng-model="vm.name" required>
                    <div class="invalid-tooltip">
                        Please enter your full name. 
                    </div>
                </div>
            </div>
            <div class="form-group row">
                <label for="validationTooltipEmail" class="col-sm-2 col-form-label">Email</label>
                <div class="input-group">
                    <div class="input-group-prepend">
                        <span class="input-group-text" id="validationTooltipUsernamePrepend">@</span>
                    </div>
                    <input type="email" class="form-control" id="validationTooltipEmail" placeholder="Email" 
                    aria-describedby="validationTooltipUsernamePrepend" ng-model="vm.email" required>
                    <div class="invalid-tooltip">
                        Please choose a valid email.
                    </div>
                </div>
            </div>

            <div class="form-group row">
              <label for="validationTooltip03" class="col-sm-2 col-form-label">Query</label>
              <div class="input-group">
                    <input type="text" class="form-control" id="validationTooltipQuery" ng-model="vm.query" placeholder="Query" required>
                    <div class="invalid-tooltip">
                        Please write your Query.
                    </div>
                </div>
            </div>
            <div class="btn-group offset-md-5">
                <button class="btn btn-primary" type="submit">Submit</button>
                <button class="btn btn-default" type="button" id="homebtn" ng-click="navigate ('home')">Home</button>  
            </div>
          </form>
          <span data-ng-bind="Message" ng-hide="hideMessage" class="sucessMsg"></span>
    </div>
</div

contact.component.js

angular.module('myApp')
  .component('contactComponent', {
  restrict: 'E',
  $scope:{},    
  templateUrl:'contact/contact.html',
  controller: contactController,
  controllerAs: 'vm',
  factory:'userService',
  $rootscope:{}
});

function contactController($scope, $state,userService) {
  var vm = this;
  vm.saveform = function(){
    var name= vm.name;
    var email= vm.email;
    var query= vm.query;
    console.log(name);
    console.log(email);
    console.log(query);



    $scope.hideMessage = false;
    $scope.Message = "Your query has been successfully submitted.";

    $scope.user = userService;
  };

  $scope.navigate = function(home){
    $state.go(home)
  };

};

//localStorage code

function userService(saveform) {
  var service = {
    model: {
        name: '',
        email: '',
        query:''
    },
    SaveState: function () {
        sessionStorage.userService = angular.toJson(service.model);
    },
    RestoreState: function () {
        service.model = angular.fromJson(sessionStorage.userService);
    }
  }
  $rootScope.$on("savestate", service.SaveState);
  $rootScope.$on("restorestate", service.RestoreState);
  return service;

  $rootScope.$on("$routeChangeStart", function (event, next, current) {
    if (sessionStorage.restorestate == "true") {
        $rootScope.$broadcast('restorestate'); //let everything know we need to restore state
        sessionStorage.restorestate = false;
    }
  });
  //let everthing know that we need to save state now.
  window.onbeforeunload = function (event) {
    $rootScope.$broadcast('savestate');
  };
};

控制台没有错误。

0 个答案:

没有答案