The functionality of ngSubmit and ngDisabled seems to be malfunctioning, although ngModel is successfully linked to the

Despite following the usual process of creating a form in AngularJS, the ngSubmit function is not working as expected and the ngDisabled feature is failing to disable the button when the fields are empty. I even tried isolating the section in a new project to rule out any external dependencies causing issues, but the problem persists even after removing unnecessary elements. If anyone can spot what's going wrong, please help me out because it seems I've been staring at this for too long without progress.

I set up the form fields through the controller to confirm their connectivity, displayed user input using pre tags bound to ng-model to track changes, and verified no errors were present with Chrome's Angular plugin. However, the login() function doesn't trigger on submission and the button remains enabled despite the required fields being empty.

Below is a simplified version of the issue that continues to malfunction:

<div ng-app="myApp">
    <div ng-controller="LoginController as loginCtrl">

        <form name="loginForm" role="form" ng-submit="loginCtrl.login()" novalidate></form>

        <div class="form-group">
            <label for="username">Username</label>
            <input type="text" class="form-control" id="username" name="username" ng-model="loginCtrl.user.username" required>
        </div>

        <div class="form-group">
            <label for="password">Password</label>
            <input type="password" class="form-control" id="password" name="password" ng-model="loginCtrl.user.password" required>
        </div>

        <div class="form-group clearfix">
            <button type="submit" class="btn btn-primary btn-block" ng-disabled="loginForm.$invalid">Login</button>
        </div>

        <pre>user data = {{loginCtrl.user | json}}</pre>

        <pre>form invalid = {{loginForm.$invalid}}</pre> <!-- always says false... but it is invalid -->

        </form>

    </div>
</div>

<script>
(function() {
    'use strict';

    angular.module('myApp', [])

    .controller('LoginController', [function() { 

        var self = this;
        self.user = { username: 'asdf', password: 'asdf' };
        self.login = function() {
            console.log("hello world");
        };
    }]);

})();    
</script>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.24/angular.min.js"></script>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>

Answer №1

Ensure that you have included the necessary Angular libraries.

Download Angular Libraries

Here are some solutions to consider: Angular.js GitHub Issue

If you want to view a working form example...

<div class="col-md-10">
    <form class="tab-pane active form-horizontal" id="first" name="userForm" novalidate ng-submit="save()">
        <h2>Sign up</h2>
        <div class="form-group" ng-class="{'has-error' : submitted && userForm.username.$invalid}">
            <label class="control-label col-md-3">Username <span class="required">* </span> </label>

            <div class="col-md-4">
                <input type="text" name="username" class="form-control" ng-model="user.username" required placeholder="username"/>
            </div>
            <p ng-show="submitted && userForm.email.$invalid" class="help-block">Username is required.</p>
        </div>

        <div class="form-group" ng-class="{'has-error' : submitted && userForm.email.$invalid}">
            <label class="control-label col-md-3">Email <span class="required">* </span> </label>

            <div class="col-md-4">
                <input type="email" name="email" class="form-control" ng-model="user.email" required placeholder="email"/>
            </div>
            <p ng-show="submitted && userForm.email.$invalid" class="help-block">Email is required.</p>
        </div>

        <div class="form-group" ng-class="{'has-error' : submitted && userForm.password.$invalid}">
            <label class="control-label col-md-3">Password <span class="required">* </span> </label>

            <div class="col-md-4">
                <input type="text" name="password" class="form-control" ng-model="user.password" required placeholder="1.2"/>
            </div>
            <p ng-show="submitted && userForm.password.$error.required" class="help-block">Password is required.</p>
        </div>

        <div class="form-group">
            <label class="control-label col-md-3">Confirm Password </label>

            <div class="col-md-4">
                <input type="text" name="password_confirmation" class="form-control" ng-model="user.password_confirmation"
                       placeholder="password_confirmation"/>
            </div>
            <p ng-show="submitted && userForm.password_confirmation.$invalid" class="help-block">Password Confirmation is required.</p>
        </div>


        <div class="form-group">
            <div class="pull-left">
                <a class="btn btn-default" href="/app/assets/images"> Back</a>
                <button type="submit" class="btn btn-primary" ng-click="submitted=true">Submit</button>
            </div>
        </div>


    </form>
</div>

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

Client/Server Approach to File Upload in Angular and NodeJS

I am currently working on a project where we are transitioning to Angular, NodeJS, Expressjs, and Couchbase. One of the tasks assigned to me is to upload various flat files to the webserver. Despite searching through different resources such as Git, npm, ...

What is the correct way to generate an await expression by utilizing recast/esprima?

I have an issue with a JavaScript function export const cleanUp = async () => { await User.destroy({ where: {} }); }; I am attempting to add a line below await User.destroy({ where: {} }) using recast.parse(`await ${module}.destroy({ where: {} } ...

Generating a preview image for a three.js environment

I am currently working on a website dedicated to visualizing the biological neurons found in animals. I have a comprehensive list of neuron names, and I use THREE JS to draw the neuron when the user clicks on its name. However, with hundreds of neurons to ...

Looking for a way to implement a vertical autoscroll <ul> list that responds to mouse movement using JavaScript and jQuery

Could you assist me in enabling the list items to be moved using mouse movement? When the user moves the mouse cursor vertically upward over the list, the list should scroll downward. If the user moves the mouse cursor downward over the list, the list shou ...

Exploring the Power of Vue 3 in Manipulating the DOM

Hello everyone, I am a beginner with Vue and I am interested in learning how to modify the DOM without relying on methods such as document.querySelector or getElementById. Let's take for instance this input: <input id="myInputId" class=& ...

Implementing an Onclick function in HTML

I have an HTML code and I am looking to add an onclick event for a button named GET DATA. When the button is clicked, I want to send data for userId and categoryId to a PHP file. Can someone help me implement this functionality in my existing code? Here ...

Learn the process of extracting data from dynamically generated elements in JavaScript

I am a beginner in Javascript and Jquery and have recently started exploring business rules with Chris Powers. https://github.com/chrisjpowers/business-rules My current project involves adding a range slider for numbers and a number spinner. I attempted us ...

Activate the content when selected

Is it possible for you to create a scenario where selecting one item makes other content active or visible, like on the Apple iPhone webpage? For example, when you choose the color of the iPhone 4, the model version becomes fully visible and interactive. ...

Having trouble with Postgres not establishing a connection with Heroku

My website is hosted on Heroku, but I keep encountering the same error message: 2018-05-06T19:28:52.212104+00:00 app[web.1]:AssertionError [ERR_ASSERTION]: false == true 2018-05-06T19:28:52.212106+00:00 app[web.1]:at Object.exports.connect (_tls_wrap.js:1 ...

javascript - The window.onload event is not firing

I am currently working on developing a tool that will allow users to export a PDF file. There are two scenarios to consider: The first scenario involves a straightforward process where the user clicks the export button, and a new blank window opens imm ...

How can one selectively apply a class to the initial DIV within a collection of dynamically generated DIV elements without relying on jQuery?

I am currently working on implementing a slideshow within a modal using AngularJS and Bootstrap. My challenge lies in dynamically creating DIVs, but specifically adding the active class to only the first DIV. Is there a way to achieve this without relying ...

Unable to display api results in React using console.log

I'm having an issue with my console log not displaying the API results. Can someone help me figure out what's wrong? I'm new to React, so any guidance would be appreciated. componentDidMount() { this.setState({ loading: true }) cons ...

Obtaining the node generated from a document fragment

Suppose I add a document fragment to the DOM in the following way: const ul = document.querySelector("ul"); const fruits = ["Apple", "Orange", "Banana", "Melon"]; const fragment = new DocumentFragment(); ...

Can someone help me figure out the best way to locate a material-ui slider within a react

I am seeking to incorporate multiple material-ui sliders into a single react component that share a common event handler. However, I have encountered difficulties in identifying which slider triggered the event. Despite referring to the API documentation, ...

How can we generate 3 different arrays, each containing an equal number of items except for the last array, using VueJS?

Incorporating Bootstrap5 and VueJS 2, I am working on designing a layout of cards in a "pinterest-style" arrangement, as depicted in the following screenshot: https://i.sstatic.net/AvdWR.png To achieve the layout showcased above, the HTML markup required ...

Express JS causing NodeJS error | "Issue with setting headers: Unable to set headers after they have been sent to the client"

As I embark on my journey to learn the fundamentals of API development, I am following a tutorial on YouTube by Ania Kubow. The tutorial utilizes three JavaScript libraries: ExpressJS, Cheerio, and Axios. While I have been able to grasp the concepts being ...

Unexpected outcome when returning a map

Encountered a puzzling issue that requires immediate clarification. When I input the following code into my project: this.metadata = json.metadata.map((x) => {return new Metadatum(x);}); console.log(this.metadata[0].value); The output consistently sho ...

Javascript encountering compatibility issues with certain versions of Internet Explorer; employing browser detection workaround

Unfortunately, Internet Explorer is causing issues when trying to execute this script (specifically the 'else' part). Despite numerous attempts, I have been unable to resolve it. $(document).ready(function(){ if (navigator.appName != "Micros ...

The Server Components render encountered a glitch

Screenshot of the errorI am encountering a strange error only in the production environment. The lack of additional information leads me to believe it may be due to security measures put in place for production. Unfortunately, I have been unable to repli ...

What is the best method for retrieving logs from one Cloud Function to another?

I am attempting to access and review the logs produced by my cloud functions. After reviewing the Firebase Documentation, I discovered a CLI command that allows us to accomplish this task: firebase functions:log This particular command displays all of the ...