Trouble with submitting a form and showing a success message in AngularJS version 1.6.8

My query submission form consists of fields for name, email, and the actual query. The component includes a controller function with a submit function to handle form submission.

To submit user input and display a success message upon submission, I utilize the ng-submit directive within the <form></form> tag.

Below are the respective files:

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,$rootScope) {
  var vm = this;
  $scope.navigate = function(home){
    $state.go(home)
  };
  $scope.saveform = function(){

    $scope.name= vm.name;
    $scope.email= vm.email;
    $scope.query= vm.email;


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

  };
  $scope.user = userService;
};

//localStorage code

function userService($rootScope) {
  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;
    }
  });
  window.onbeforeunload = function (event) {
    $rootScope.$broadcast('savestate');
  };
};

UPDATE: Upon submitting the form, when checking the response in the network tab of dev tools, only the markup is visible without the submitted values.

Answer №1

When setting up your template, make sure to use the method name saveform:

ng-submit="saveform()"

However, in your controller, the method is actually named save:

$scope.save = function() { ... }

To avoid confusion, rename it to saveform:

$scope.saveform = function() { ... }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to retrieve content from a different website using javascript in an asp.net environment?

Currently working on a web application in asp.net where I want to provide users with a JavaScript code that allows them to fetch content from my website and display it on their own website. To handle user requests on my website, I have implemented a gener ...

Why won't JavaScript functions within the same file recognize each other?

So I have multiple functions defined in scriptA.js: exports.performAction = async (a, b, c) => { return Promise.all(a.map(item => performAnotherAction(item, b, c) ) ) } exports.performAnotherAction = async (item, b, c) => { console.log(`$ ...

When attempting to display the image file path using the JavaScript API Notification in Angular, there is a challenge in

Hello fellow developers, I am currently facing an issue with retrieving a local image from my assets folder and displaying it on a new Notification object generated by the Web Notification API. I have an image in my assets folder with a .jpeg extension. ...

Tips on creating a responsive absolute div

I'm currently working on creating a profile component with Material UI and React.js. I'm having trouble making the Avatar/profile photo and profile name div responsive. Here are some screenshots to illustrate my issue: The specific div that need ...

AngularJS is not displaying the full value of the model in the user interface

I have a technique for saving an object into an array after modifying some of its properties. However, I am facing an issue where not all the modified properties reflect in the user interface. Code : HomeController.js $scope.MainArray=[]; $scope.newIt ...

Limited access textbox

Is there a way to create a text-box in AngularJS/HTML that is partially readonly? For instance, having the default value as "+91" and making it readonly while allowing users to enter additional values afterwards? ...

Tips on ensuring Angular calls you back once the view is ready

My issue arises when I update a dropdown list on one of my pages and need to trigger a refresh method on this dropdown upon updating the items. Unfortunately, I am unsure how to capture an event for this specific scenario. It seems like enlisting Angular ...

Experiencing unexpected outcomes via AJAX requests

Linked to: Query Database with Javascript and PHP This inquiry is connected to my previous question. I made adjustments to the PHP script based on one of the responses I received; however, when attempting to utilize $.getJSON, I encountered difficulties. ...

Error in Angular Universal: KeyboardEvent is not defined

I have inserted the word "domino" into the server.ts file and updated the webpack.server.config.js as follows: module: { rules: [ { test: /\.(ts|js)$/, loader: 'regexp-replace-loader', options: { match: { pattern: '&bs ...

Can you explain what an express template engine is?

Having recently started working with express and angular development, I have a question about rendering views. Specifically: 1) What are the advantages of rendering a view via express using a template engine rather than solely relying on angular? 2) Is i ...

How can I modify the appearance of a slider's range?

I'm struggling with customizing the style of a price slider input range. No matter what I do, the changes don't seem to take effect. Can anyone pinpoint where I may be going wrong? Appreciate any guidance on this! Thank you! <div class=" ...

Tips on maximizing efficiency in number game coding

Seeking to create a number using a specified set of 6+ inputs. For instance, aiming for the number 280 with inputs [2,4,5,10,30,50,66], the desired output format would be something like this: ((2+5) * 4 * 10). Each input number can only be used once per s ...

Using AngularJS to Bind a $scope Variable

As I work on constructing a directive in angularJS, I come across an issue where I am attempting to bind an object property from another variable to an HTML element. Here is an example: angular.module('ng.box', codeHive.angular.modules) .directi ...

Retrieve the data from multiple forms effortlessly with a single click

I'm currently working on getting the results for multiple forms simultaneously. I have a structure in place to retrieve the result one by one, but now I need to send the results of all of them together. Here is my HTML code snippet: <form (ngSubmi ...

Is there a way to assign a value to an Angular-specific variable using PHP?

In the development of my Angular 4 application, I encountered an issue while receiving JSON data based on an id value through a PHP script. Upon examining the code, it seems that there should be a value passed into this.PropertiesList. examineProperties(i ...

Animation does not occur after the stop() function is executed

My goal is to create a functionality where, upon moving back to the grey content box after leaving the button, the slideUp animation stops and the content slides down again. This works seamlessly with jQuery 1.x (edge), but when I switch to jQuery 1.10, th ...

What is the process for including a new item in an array of objects?

const data = [ { title: 'Tasks', items: ['complete assignments', 'study for exams'], }, { title: 'Ongoing', items: ['learn new skills', 'work on projects'], }, { titl ...

Encountering a Peer dependency problem while executing node within a docker environment

Currently, I am utilizing `node-pg-migrate`, which has a peer dependency on `pg`. Here is an excerpt from the library's `package.json` file: "peerDependencies": { "pg": "^4.3.0" }, My attempt to execute the application in Docker involves the fo ...

Utilizing jQuery in Wordpress to Toggle Content Visibility

Currently, my website pulls the 12 most recent posts from a specific category and displays them as links with the post thumbnail image as the link image. To see an example in action, visit What I am aiming to achieve is to enhance the functionality of my ...

What is the best way to style the current element being targeted in React?

In my React application, I am trying to dynamically adjust the height of a textarea element based on its content. I want to achieve this by utilizing an 'onchange' listener to trigger a resize function. While I have successfully implemented the ...