Tracking changes in an email input using Angular's $watch functionality

I am currently working on creating a custom directive to manage form input errors. Here is what I have so far:

directive('input', function() {
    return {
        restrict: 'E',
        scope: true,
        link: function (scope, element, attrs, ctrls) {
            var name = attrs.name;
            scope.$watch(attrs.ngModel, function () {
                console.log(name + ' has changed');
            });
        }
    };
});

And in the HTML:

<input type="text" ng-model="data.text">

This setup allows me to monitor any form errors within the $watch callback.

However, there seems to be an issue with input[type="email"] fields that also have a required attribute. I cannot seem to determine why the $watch callback does not fire in this scenario.

To illustrate this problem with different input components, here is an example: http://jsfiddle.net/g0atq0z4/

Does anyone have any suggestions on how to detect changes in ngModel in this specific case?

Answer №1

If you want to trigger the event after the "@" symbol, try using "abc@"

It's also recommended to use ng-change if you need to detect any changes in the input field.

You can check out this fiddle for reference: http://jsfiddle.net/p0qo4qst/

angular.module('app', [])

.controller('TestCtrl', function ($scope) {

    $scope.changed = function (){
        console.log('changed');
    };

});

In addition, consider placing inputs inside a form element as it will give you access to $dirty, $valid, and $invalid states.

For more information, visit: https://docs.angularjs.org/api/ng/type/form.FormController

$dirty - boolean True if user has interacted with the form.

$valid - boolean True if all forms and controls within are valid.

$invalid - boolean True if at least one control or form is invalid.

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

Use href id to navigate back to the top of the page by

I am trying to implement a function that scrolls the div to the top position. However, I am encountering an issue with retrieving the href value. When I use 'a' in console.log(a);, it returns undefined. function myFunction() { var a=$ ...

Iterating over an array while postponing a function

My goal is to create a continuous loop through an array of number values. These values will be used as delay parameters in a setInterval function, triggering another function each time. Here's what I've come up with: HTML: <p>On</p> ...

Generating JSON objects within the Ionic SQLite framework

I am new to Ionic development and I'm looking for a way to convert a JSON string into a JSON object in Ionic, as well as how to access this JSON data on an HTML page. controller.js app.controller('OilTrackerListingCntrl', function($scope, ...

Having trouble muting the audio on my Vue audio player

I'm facing some challenges with muting the audio within my vue app. I have a list of songs that can be played, paused, shuffled, etc., but I can't seem to get the mute function working. Here's what I have in the JavaScript: mute() ...

The flow of browsing the web and executing scripts in a web browser

I'm really struggling to grasp the logic behind this function I'm working on. public void PortalLogin(AutoResetEvent signal) { // Navigate to portal string portalUrl = "website_name"; ...

Seeking advice on modifying the background color within the code? (Designer delving into the coding realm)

Seeking guidance on changing the background color within this code. (designer navigating the coding realm) <script src="https://cdn.rawgit.com/mrdoob/three.js/fdefb19b/examples/js/renderers/CanvasRenderer.js"></script> <script src="https ...

Triggering jQuery events can be customized by excluding certain elements using the

Is there a way to hide the div "popu" when clicking on the img "tri"? I've tried using .not() since the img is a child of the div popu, but it didn't work. Also, I need to make sure that clicking on the div "textb" does not trigger the hide actio ...

"The controller seems to always return an 'undefined' value for the Angular

Within my project, I have implemented ui-view with the following structure: <main class="content"> <div class="inner" ng-controller="OrderPageController"> <div ui-view="main-info"></div> <div ui-view="comment ...

Is it possible to invoke a Django view function in JavaScript by using its namespace and function name?

If we want to call a Django view from HTML, we typically use the syntax below: "{% url 'namespace: view_fun' %}" But what about calling it from JavaScript? Your assistance is greatly appreciated! ...

Inconsistent behavior of transform scale() between Chrome and Safari upon page refresh

My goal was to design a code that would adjust all content according to the browser size, ensuring that everything remains visible even when the window is resized. var docHeight = $(document).height(); var winHeight; var zoomRatio; function z(number) { ...

Unable to show certain special characters when using ng-repeat

I am attempting to showcase an array of objects using ng-repeat. Below is the collection of objects $scope.test = [ { value1: "app\test\maintenance1", value2: "other value1" }, { value1: "app\test\m ...

Top recommendations for developing with AngularJS

In my extensive angularjs application, I have organized it using a module pattern as shown below: ./ /search search.js /admin /users users.js /settings settings.js admin.js /edit edit.js /common /components /modal ...

dividing JavaScript files in the three.js game development platform

After spending two years teaching myself unity/c#, I believe I have the coding skills to transition from an engine to a framework for better control over the environment. As I started working on first person environment features, I wanted to organize my co ...

What is the best way to ensure a navigation link stops scrolling at the bottom of a navbar that is set to position: sticky?

I am utilizing Bootstrap 5 for my website, and I have a sticky-top attribute on my navbar. When I click on one of the links in the navigation bar, the page scrolls to that section, but some of the content goes behind the navbar. I am looking for a way to m ...

Node.js: Implementing user redirection after successfully signing up/login on a nodejs platform

I am having an issue with redirecting a user to their profile page in node.js after they login or sign up. The login/signup system is functioning correctly and the data is being saved, but the user is not getting redirected to the correct profile page ro ...

css three columns evenly spaced in the center

I am currently using bootstrap, CSS, and HTML to design a responsive webpage with three columns in a row. The layout consists of a textbox on the left, a button in the center, and a table on the right. So far, I have made three separate attempts at achiev ...

Tips for avoiding multiple reference paths in Angular TypeScript: - Simplify your imports

Setting up Typescript for an Angular 1.5 application has been a bit of a challenge. To ensure that a TS file can be compiled by gulp without any errors, I have to include the following line: ///<reference path="../../../../typings/angularjs/angular.d.t ...

What is the best way to embed a variable within a Directive HTML block for seamless access by the Controller?

I am facing a challenge with my custom directive that inserts an HTML block onto the page. The issue is to have a variable within this block that can be manipulated based on an ng-click function in my controller. This is what my directive looks like: .di ...

Simultaneously sending jQuery.ajax data while submitting a form

I'm facing a bit of a dilemma here. I have a form with multiple fields, including one for entering links. When you input a link and click the add button, the link is added to a link_array using jQuery. My goal is to send this array through the jQuery. ...

Tips for integrating CSS keyframes in MUI v5 (using emotion)

Hey there! I'm currently working on adding some spinning text, similar to a carousel, using keyframes in my React app. The setup involves MUI v5 with @emotion. Basically, I want a "box" element to show different words every few seconds with a rotating ...