Issue with AngularJS email input field not clearing or resetting after model binding is reset

I'm currently working on a form that includes fields for name, email, and phone number.

    <div ng-show="Nerd.adding">
    <form class="col-sm-6" name="Nerd.nerdAddFrm" novalidate >
        <div class="form-group">
            <label for="inputName">Name</label>
            <input type="text" class="form-control" id="inputName" placeholder="Name" ng-model="Nerd.nerd.name" required >
        </div>
        <div class="form-group">
            <label for="inputEmail">Email</label>
            <input type="email" class="form-control" id="inputEmail" placeholder="Email" ng-model="Nerd.nerd.email" required >
        </div>
        <div class="form-group">
            <label for="inputPhone">Phone</label>
            <input type="text" class="form-control" id="inputPhone" placeholder="Phone" ng-model="Nerd.nerd.phone" required >
        </div>
        <button ng-click="Nerd.saveNerd(Nerd.nerd)"  type="submit" class="btn btn-primary">Submit</button>
        <button ng-click="Nerd.load()"  type="button" class="btn btn-default">Cancel</button>
    </form>
</div>

In the above code snippet, you can see that clicking the cancel button triggers the Nerd.load() function in the controller. This function resets the view and clears all the data binded to the model.

    Nerd.load = function () {
    Nerd.editing = false;
    Nerd.adding = false;
    Nerd.nerd = [];
    nerdResource.query(
        function (data) {
            Nerd.nerds = data;
        }
    );
};

I've noticed that when I set Nerd.nerd to an empty array, it successfully empties out the data for Name and Phone fields. However, upon returning to the page, the previously inputted data still appears. The page doesn't reload, as divs are shown or hidden based on controller variables like

<div ng-show="Nerd.adding">
. Can someone provide assistance with this issue?

My AngularJS version is 1.3.14. Any guidance on resolving this matter would be greatly appreciated.

Thank you.

Answer №1

To properly utilize these variables, you must assign them to your $scope in the following manner:

 $scope.Nerd.load = function () {
    $scope.Nerd.editing = false;
    $scope.Nerd.adding = false;
    $scope.Nerd.nerd = [];
    nerdResource.query(
    function (data) {
        $scope.Nerd.nerds = data;
    }
    );
};

Additionally, I suggest initializing $scope.Nerd as an empty object like this:

$scope.Nerd = {};

Instead of assigning it as an empty array. Remember to use $scope when communicating with the view. The current form of this code does not align with Angular practices.

Answer №2

If you attempt to follow a certain method.

Nerd.load = function () {
    Nerd.editing = false;
    Nerd.adding = false;
    Nerd.nerd = [];
    nerdResource.query(
        function (data) {
            Nerd.nerds = data;
            Nerd.nerd = []; // Assign an empty array here
        }
    );
};

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 update the text within a Span element as the user moves through the Jquery slider?

Can I utilize jQquery to dynamically alter the text content of my "video_box_label" span element based on the active slide in my "flexslider" slideshow? For instance, when the slideshow transitions to the second slide, I want the text to change from "Mee ...

What advantages does utilizing an angular directive provide in the context of a comment?

Up to now, I have primarily used Directives in the form of Elements or Attributes. Is the Comment style directive simply a matter of personal preference for style? app.directive('heading', [function () { return { replace: true, ...

Custom HTML on Joomla causing carousel to vanish

I've encountered an issue with my Joomla website where my carousel images are disappearing. I have a code snippet from W3 Schools for an automatic banner that works perfectly fine when opened in a browser using Notepad++, but when inserted into a cust ...

Fade in text and then vanish after a few moments

I am looking to implement a fading in and out effect for text that stops after a certain period of time. The goal is for the text to indicate that a user is online when they join, and then disappear after some time. Currently, the function does not have a ...

Combining formData props with Component state in React using Redux

Monitoring form input changes and saving them to the state in my Editor Component. Upon form submission, dispatching the formData from the state to the store. Upon Component reloads, intending to merge the prop's formData with the state. Attempted u ...

AngularJS: Troubleshooting a Service Function with a Dysfunctional For/In

I am facing an issue with a for/in loop within a function that is not functioning properly. $scope.items = []; jobslisting.getJobs(function(data){ for(var i = 0; i < data.length; i++){ $scope.items.push({name:data[i]}); } con ...

Storing the current date and time in a MySQL database using NodeJs

I'm having an issue inserting a DATETIME field into my MySQL database. var dt = require('moment')().format('YYYY-MM-DD HH:mm:ss'); pool.query( `insert into login(id, id_utente, data_login) values(NULL,?,?)`, [result ...

Having trouble rendering components due to conflicts between React Router and Semantic UI React

Below is the code in my App.js: import { useRecoilValue } from 'recoil'; import { authState } from './atoms/authAtom'; import { ToastContainer } from 'react-toastify'; import { ProtectedRoute } from './layout/ProtectedRou ...

Caution: Always remember to surround any utilization of a keyed object

I can't figure out what's causing the warning in my code. It says Any use of a keyed object should be wrapped class GreetingsComponent extends React.Component { constructor(){ super() this.handleInput = this.handleInput.bind(this) ...

What is the process for adding variables to a context menu using AngularJS? (By right-clicking

Can anyone help me out here? I came across a code snippet that I need to use, but it's incomplete. I'm trying to add variables to the edit function for a right click menu, but I'm not sure how to do it. For instance, how can I pass the val ...

The image is loaded correctly through the image picker, however, it is not displaying on the screen

When I click the button to pick an image from the gallery in this code, it is supposed to load the phone's gallery and display the selected image in the image component. Even though the image gets loaded properly (confirmed through test logs), it does ...

Can an element be dynamically bound and unbound upon clicking a button in AngularJS?

After creating a display text and an input field, I bound them together using ng-model in the code below: HTML <div ng-app ng-controller="test"> <div ng-bind="content"></div> <input id="txtElem" type="text" ng-model="content" ...

Contrast and combine information from two JavaScript arrays of objects

I am struggling with comparing two arrays of objects based on a key. My goal is to compare the values, subtracting them when the keys match and displaying negative values for those not found in my target array. Additionally, I want to include all target o ...

Getting rid of background color in a tooltip using Material UI

I'm currently tackling an issue with Material UI's tooltip. I can't seem to find a way to make the background of the tooltip completely transparent. By default, it displays with a grey background and white text. Changing the background color ...

Struggling to make Bootstrap-5 scripts and Rails6 cooperate, even after extensive research and troubleshooting

Hey there, I'm a complete beginner when it comes to JS, Bootstrap, and webpack, although I am familiar with HTML, (S)CSS, PHP, and currently diving into RoR. My current challenge is connecting Bootstrap-5 to Rails-6. Despite reading numerous blog pos ...

Tips for displaying average progress using a progress bar

I'm in the process of creating a website and I would like to incorporate a progress bar that reflects the average of all the input numbers obtained from an HTML form. Although I have some experience with JavaScript, I am currently in need of the Java ...

Attempting to create a clickable thumbnail for a YouTube video that expands in size when selected

Seeking a method to create thumbnail images of YouTube videos embedded in HTML that can expand to full size when clicked. The goal is to have a row of video thumbnails with this functionality. For example, one of my videos at http://www.youtube.com/embed/ ...

Issues with VS Code detecting inline JavaScript variables in an HTML file when referenced in a TypeScript file

In my index.html file, there is an inline script containing various variables... <body> <div id="load-form-here"></div> <script> let formID="abc123" let myBool = true let myArray = ["foo" ...

Leveraging Directives for Enhanced Leaflet Marker Functionality

Currently, I am conducting experiments involving AngularJS and Leaflet (I am still in the learning process with both). As part of this experimentation, I have noticed that I can define some HTML as a parameter in markers.bindPopup(...);. Has anyone attem ...

What are the steps to generate interactive hotspots in a 3D setting with threejs?

I'm new to working with three.js and I'm attempting to create a project similar to the example found at . I have everything set up using three.js, but I'm struggling to figure out how to implement a hotspot (like the red dot in the provided ...