The form in the AngularJs function cannot be recognized

I have been working on creating a form that allows users to change their username. Below is the HTML code I have written:

<div ng-controller="manageProfile" >
    <form>
        <div class="form-group">
            <label>Display Name</label>
                <input type="text" ng-model="user.currentName" placeholder="Display Name">
        </div>
        <button ng-click="changeName()">Change Username</button>
    </form>
</div>

Here is my AngularJs code:

var profile = angular.module('getUserInfo', ['ngParse']);
profile.controller('manageProfile', ['$scope', function($scope) {

    $scope.changeName = function(form) {
        var user = Parse.User.current();
        user.setUsername(form.currentName);
        user.save(null, {
            success: function(user) {
                console.log("Success");
            },
            error: function(error) {
                console.log("Fail");
            }
        });
     };
}]);

Upon clicking the Change Username button, I encountered an error message stating: Error: form is undefined. Can someone help me understand what went wrong and how I can fix it?

Answer №1

The parameter for a click event is "$event". You did not pass any parameters to the changeName function in your HTML code. Using ng-model will allow you to retrieve values from a form after assigning ng-model in the HTML.

 $scope.updateUsername = function() {
        var currentUser = Parse.User.current();
        currentUser.setUsername($scope.user.newName);
        currentUser.save(null, {
            success: function(user) {
                console.log("Update successful");
            },
            error: function(error) {
                console.log("Update failed");
            }
        });
     };

Answer №2

Furthermore, your form requires a designated name for Angular to recognize it.

<form name="myForm" />

Subsequently, you can retrieve it from your controller using-

var form = $scope.myForm;

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

Is there a way to set the default accordion to automatically open the first element?

Incorporating bootstrap accordion into my project has been quite beneficial. Check out the working example on this fiddler link. Below is the HTML code snippet: <div ng-app="myapp"> <div ng-controller="AccordionDemoCtrl"> <uib-accor ...

Designing access to data in JavaScript

As I work on developing my single page app, I find myself diving into the data access layer for the client-side. In this process, a question arises regarding the optimal design approach. While I am aware that JavaScript is callback-centric, I can't he ...

What is the process for integrating GitHub repository code into my client-side JavaScript application?

I am attempting to incorporate the GitHub repository "zipcelx" into my client-side JavaScript, but all I see is an option to download it from npm, which I do not understand. It would be helpful if someone could explain how a module meant for client-side us ...

Utilizing client-sessions in node.js without the need for express framework

I'm currently attempting to implement client-sessions without using express, but I'm struggling with properly adapting the example code. var sessionOptions = { cookieName: 'mySession', secret: 'blargadeeblargblarg', durat ...

Trouble with Setting Up the Email Address for the 'File a Complaint' Form in WordPress

I am currently in the process of integrating a 'Complaint Registration Form' on my WordPress site. This form enables users to input their name, email address, order ID, and reason for filing a complaint. Once submitted, the details are forwarded ...

Personalized payment fields: Revealing/concealing data using a selector

Following this successful solution: // Incorporating an external jQuery/JS file function cfields_scripts() { // IMPORTANT NOTE: // When using a child theme, replace get_template_directory_uri() with get_stylesheet_directory_uri() // Place th ...

instead of downloading the file, click on the provided download link to open it directly

Currently, I am utilizing Python Selenium to web scrape images in Chrome. To complete the download of these images, I generate download links using the subsequent code snippet: script_js = 'var imageURL = document.getElementsByTagName("{select_ta ...

When selecting an option triggers a pop-up in JavaScript

Implementing javascript and HTML. Consider this example: <select name='test' > <option value='1'> <option value='2'> <option value='3'> </select> If the user selects optio ...

Issue with GetText() function: xpath is failing to retrieve the desired value

Snippet of HTML Code: <strong class="text-xl ng-binding" ng-bind="summary.PublishedIssuedRecent">5</strong> My XPath Query: @FindBy(how = How.XPATH, using = "//strong[@ng-bind='summary.PublishedIssuedRecent']") public WebElement co ...

What is the best way to transform a JavaScript object into an array?

Here is the information I have: {product_quantity: {quantity: 13, code: "AAA", warehouse: "1000",}} The product_quantity field is part of a JSON object in a MongoDB database. I am looking to convert it into this format: {"produ ...

Unexpected behavior with MongoDb $filter when using $or

Please click on this link to access the interactive coding playground and code along with me: playground I have a collection called Pages where each document contains keys Urls and DraftUrls, which are represented as an array of objects listed below: In ...

The table row dissolves instead of disappearing for a specific model

Currently, I am in the process of implementing a live search feature. The aim is to have the elements of a table fade out if they do not match the specified filter and fade in if they do match. Unfortunately, the following code snippet is not achieving thi ...

Position a div next to a table and include scrollbars

My challenge is to create a responsive layout with a table and a side div, as shown in the image below. I attempted to use CSS float properties for the table and side div, but it did not provide the responsiveness I needed. I then tried implementing Bootst ...

Prevent Purchase Button & Implement Modal on Store Page if Minimum Requirement is not Achieved

On my woocommerce shop page, I am facing an issue where all items are added to the mini-cart without meeting the minimum order requirement. This results in users being able to proceed to checkout without adding enough items to meet the minimum order amount ...

Altering the language code on LinkedIn

As a beginner in programming, I successfully added the linkedin share button to my test webpage. Now, I am hoping to make the button change language based on the user's language selection on the webpage. Linkedin uses a five character language code ( ...

Tips on inserting elements from an array into <img src=""> and <a href=""> HTML tags

I have an array of items stored in a variable called test. For example: var test = img; The value held in test[0] is retrieved from the database as /images/gallery/1.jpp Now, I want to insert the value of test[0] into both the img src="" and href="" at ...

ng-tags-input not functioning as expected with autocomplete feature

When I add a tag by selecting from a list populated using an $http request, the tag is added but the text that I have typed remains with an ng-invalid-tag class. Screenshots: 1) Initially: https://i.sstatic.net/g7K4U.png 2) Typing 3 letters to make an H ...

Verification of user input field

For this mini deposit app, I needed to implement validation for the input field to check for three different conditions: no blank entries, only numerical values, and no negative numbers. Despite having the functionality, my attempts at implementing validat ...

Creating HTML tabs using jQuery with input[type="radio"] tutorial for beginners

I'm having trouble creating a tab with input[type="radio"] using Jquery. The tab works fine on the first click, but it doesn't work on the second click. I can't figure out where the issue is in the Jquery code. Can someone assist m ...

Pressing the button twice is necessary for troubleshooting when initially opened

After creating a code snippet that hides or shows part of the text when a button is clicked, I noticed that upon launching the page, it requires two clicks on the button to function properly. This issue occurs only during the initial launch, as after the f ...