The initialization of the ng-model to an empty string as the default value

I am currently working on a user login and registration form where I noticed that the default values appearing are most likely cached by my browser. I am exploring ways to clear these values programmatically so that the input boxes are empty. Despite attempting to set the $scope values to empty strings, the issue persists even when I refresh the page. The image below illustrates the problem after a page refresh: https://i.sstatic.net/MZHuZ.png

How can I ensure that the input boxes are blank?

Below is the HTML code snippet:

<div class="root" ng-controller="userController"> 
    <div class=user>
        <form name="login_form" >
            <h2 class>Login</h2>
            <h3 class = "login_page">UserName</h3>
            <input ng-model="user" type="text" ng-minlength="1" required>
            <h3 class = "login_page">Password</h3>
            <input ng-model="password" type="password" name="password" ng-minlength="4" required>
            <input type="submit" value="Login" ng-click="login()" >
            <div ng-if ="login_form.$submitted" ng-messages="login_form.password.$error" style="color:maroon" role="alert">
                <div ng-message="minlength">Your field is too short</div>
            </div>
            <p ng-if="error">Username or login is incorrect</p>
        </form>
    </div>
    <div class=user>
        <form name = "register_form">
            <h2 class>Register</h2>
            <h3 class = "login_page">UserName</h3>
            <input ng-model="reg.name" type="text" required>
            <h3 class = "login_page">Password</h3>
            <input ng-model="reg.password" type="password">
            <input type="submit" value="Register" ng-click="register()" required >
            <p ng-if="small">Password must be at least 5 chars long</p>
            <p ng-if="duplicate">That user name is taken, please choose another</p>
            <p ng-if="correct">Registration Succesfull</p>
        </form>
    </div>
</div>

And here is the corresponding JavaScript controller code:

app.controller('userController', ['$scope', '$location', 'userFactory',  function($scope, $location, userFactory){
    index = function(){
        userFactory.set_name(function(returned_data){
            $scope.reg = {name:''};
            erase();
        })
    }
    index();
    $scope.login = function(){
        userFactory.login($scope.user, $scope.password, function(sendUser){
            if(!sendUser.data.error){
                $location.url('/logged_in');
            }else {
                $scope.error = true;
                erase();
            }       
        })  
    }
    $scope.register = function(){
        $scope.small = false;
        if($scope.reg.password.length < 5){
            $scope.small = true;
        }else {
            userFactory.register($scope.reg.name, $scope.reg.password, function(sendUser){
            console.log(sendUser)
            $scope.duplicate = false;
            $scope.correct = false;
            if(sendUser.data.error){
                $scope.duplicate = true;
                erase();
            }else {
                $scope.correct = true;
                erase();
            }
        })
        }
    }
    erase = function(){
        $scope.reg.name = "";
        $scope.reg.password = "";
        $scope.user = "";
        $scope.password = "";
    }

}]);

I have also attempted using ng-init="user=''" in the input tag for the user, however, this approach was also unsuccessful.

Answer №1

Developers are at odds with browser vendors in the ongoing battle to find a reliable solution to prevent autofill. One suggestion from browser vendors is for users to manually disable autofill for specific websites in their browser settings;

Disabling Chrome Autofill

An experimental approach that was previously tested involves creating a directive called prevent-autofill, which dynamically changes the input type during runtime and resets it on keyDown events.

Answer №2

Have you attempted to use ng-init="user=''"

If that method is not successful, you can try the following:

<input ng-model="password" type="password" name="password" ng-minlength="4" required autocomplete="nope">

By implementing this, you will be disabling the browser's autocomplete feature. For more information, please visit this link

Answer №3

By adding autocomplete="off" in this section, it serves a dual purpose:

  • It prevents the browser from storing field data for future autocompletion on similar forms using different algorithms depending on the browser.
  • It prevents the browser from caching form data in the session history. This means that if the form data is cached, the user's input will still be visible even after submitting the form and using the Back button to return to the original form page.

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

Creating dynamic scroll animations for sidebar navigation in a single-page website with anchor links

I need help creating a seamless transition between anchor points on a single page, while keeping a fixed navigation menu that highlights the active section. As a novice, I am unsure how to incorporate "( document.body ).animate" or any other necessary code ...

Troubleshooting Problems with Retrieving Data Using jQuery and

Having trouble fetching information from a JSON file, as the data variable is empty. I have already downloaded the JSON file, so it's not an issue with the server connection. Can anyone point out where I might be going wrong? function handle_geolocat ...

Increase in JQuery .ajax timeout not effective

My website has a process where JavaScript sends a POST request to a PHP server using the .ajax() function. The PHP server then communicates with a third-party API to perform text analysis tasks. After submitting the job, the PHP server waits for a minute b ...

When should the grecaptcha.execute() function be invoked while utilizing Invisible reCAPTCHA V2?

I recently implemented invisible reCAPTCHA successfully, but I'm wondering if I did it correctly when calling grecaptcha.execute(). After loading the API script with an explicit call like this: <script src="https://www.google.com/recaptcha/api.js ...

How can I dynamically update the URL parameter based on the selected option in a dropdown list?

There is a single select option box with three options. When a user selects an option, the corresponding value should be appended to the URL (e.g., value=needbyDate). If another option is selected later, the previous value in the URL should be replaced w ...

A guide to correctly importing a Json File into Three.js

I've been working on some cool projects in Blender and wanted to showcase one using threejs. However, I'm facing an issue where the object isn't displaying properly. Can someone guide me on how to correctly load a JSON file with keyframe ani ...

The output of JSTree's data.rslt.obj.text() function is an array of text values, not just the text from the specified node

I am facing an issue with the jstree where it is returning a concatenated list of node names when I try to rename a node, instead of just the text for the node I am renaming. The jstree is set up to load on demand. How can I ensure that only the text for ...

Modifying an image with jQuery

Why won't this image change? Below is the relevant HTML, CSS, and jQuery code. HTML <nav id="desktop-nav"> <div class="logo"> <a href="#"></a> </div> <div> ... </div> ... CSS nav#desktop-nav . ...

What is the best way to fetch a partial JSON response object from an API using JavaScript?

Currently, I am utilizing the One Bus Away API, returning a response object in this format: { "code": 200, "currentTime": 1504150060044, "data": { "entry": { "arrivalsAndDepartures": [ {AnD_item1 ...

Guide to forming a JavaScript array from nested JSON data

Looking to parse through an array of objects related to London weather data from the OpenWeatherMap API. How can I create three distinct arrays in JavaScript, each with variable names "dtArray", "tempMinArray", and "tempMaxArray", containing only values f ...

Dispatching information to a designated Google Analytics tracking code

Within our website, we have a unique dimension that is configured on Google Analytics and utilized when sending the page view event: gtag('config', 'UA-TrackingCode', { 'custom_map': { 'dimension1': &apo ...

Error: The property 'scrollIntoView' cannot be read because it is null

class MessageApp extends Component { constructor(props) { super(props) this.state = { text: "", messages: [] } } componentDidMount() { const config = { apiKey: "<api-key>", authDomain: "<projec ...

When the enter key is pressed, the form will be submitted and the results will be displayed in a modal window without

Behold, my unique form! It lacks the traditional <form></form> tags. <input id="query" name="query" style="font-size: 18pt" id="text" data-email="required" type="text" placeholder="Search Here from <?php echo $row['no']."+"; ?& ...

Error: Although precheck is successful, the Chrome API Runtime has exceeded the QUOTA_BYTES_PER_ITEM quota

I keep receiving a QUOTA_BYTES_PER_ITEM error when attempting to store an object, even though my size precheck shows that it should be under the quota. I must be missing something simple here (is this method correct for checking object size?). I've al ...

Webpack2 now transforms sass/scss files into JavaScript rather than CSS during compilation

I've created a webpack script to compile all .scss files into one css file. I decided to use webpack instead of gulp or grunt for simplicity, as it can be configured in a single file. However, I am encountering an issue where scss files are being com ...

Synchronous async routes in Node Express

My express server requires fetching data from multiple external sources for each request, with the logic separated into various routers (some of which are not under my control). These routers operate independently, eliminating the need for one to wait on ...

What is the method for including a data attribute in the text of a cell using Datatables Editor?

When initializing datatables along with editor, I typically use the following code: columns: [ { data: "name", className: 'noEdit clickTextToOpenModal' }, In this line of code, I am able to set the className attribute. However, is i ...

Tips for personalizing the error message displayed on Webpack's overlay

Is there a way to personalize the error overlay output message in order to hide any references to loaders, as shown in this image: https://i.sstatic.net/ESFVc.png Any suggestions on how to remove the line similar to the one above from the overlay output? ...

Utilizing Ionic to import and parse an Excel file for data processing

I need assistance in uploading an Excel file and reading it using Ionic-Angular. I tried the following code, but it only seems to work for browsers and not for the Ionic app on Android. Can someone please help me with this issue? I am trying to read the E ...

The error message in Express points to module.js line 550 and states that the module cannot be

I am currently in the process of setting up a basic express application using the code below: const express = require('express'); const app = express() const bodyParser = require('body-parser'); const cookieParser = require('cooki ...