Custom directive not invoking function in $parsers-array

To monitor the change of a value in a customized directive, I am utilizing the $parsers unshift-function to incorporate my own function.

Unfortunately, my custom function is not being triggered!

Below is the code snippet for my view:

<div ng-controller="MyCtrl">
    <form novalidate name="myForm">
        Number: <even-number name="awesomeField" ng-model="val"></even-number>
    </form>
</div>

This is my JavaScript code:

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.val = "42";
    $scope.$watch('val', function() {
        console.log("Controller: "+$scope.val);
    });
}

myApp.directive('evenNumber', function(){
    var tmplt = ''+
    '<div class="input-group">'+
        '<input class="form-control" name="inputDate" type="text" data-ng-model="ngModel"/>'+
        '<span class="input-group-btn">'+
            '<button class="btn btn-default">Default</button>'+
        '</span>'+
    '</div>';
    return {
        restrict: 'E',
        require:'ngModel',
        replace: true,
        template: tmplt,
        scope: {
            ngModel: "="
        },
        link: function(scope, elem, attrs, ctrl){
            ctrl.$parsers.unshift(checkValue);

            function checkValue(viewValue){
                console.log("halllllo");
                return viewValue;
            }
        } // end link
    }; // end return
});

Any idea on what could be causing this issue?

See the complete scenario in this jsFiddle

Answer №1

Develop your own custom directive as an attribute that can be applied to the <input /> element, leveraging the existing functionality of ngModel. Simply wrapping the input won't work with ngModel - you'll need two directives for this task. One to group the input and button together (even-number) and another to modify the input itself using $parsers for model --> view conversion & $formatters for view --> model conversion.

Update: Check out this functioning example below:

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.val = "42";
    $scope.$watch('val', function() {
        console.log("Controller: "+$scope.val);
    });
}

myApp
.directive('evenNumberConverter', function(){
    return {
        restrict: 'A',
        require:'ngModel',
        link: function(scope, elem, attrs, ctrl){
            ctrl.$parsers.unshift(checkValue);
       
            function checkValue(viewValue){
                console.log("hello");
                return viewValue;
            }
        } // end link
    }; // end return
})
.directive('evenNumber', function(){
    var tmplt = ''+
    '<div class="input-group">'+
        '<input class="form-control" name="inputDate" type="text" even-number-converter data-ng-model="value"/>'+
        '<span class="input-group-btn">'+
            '<button class="btn btn-default" data-ng-click="setDefault()">Default</button>'+
        '</span>'+
    '</div>';
    return {
        restrict: 'E',
        replace: true,
        template: tmplt,
        scope: {
            value: "="
        },
        link: function(scope, elem, attrs, ctrl){
            scope.setDefault = function() {
              scope.value = 0;
            };
        } // end link
    }; // end return
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
    <form novalidate name="myForm">
        Number ({{val}}): <even-number name="awesomeField" value="val"></even-number>
    </form>
</div>

Notes: Your directive doesn't utilize any of the methods from ngModelController to manage the value between the view component and model. It acts merely as a "wrapper" directive, where the ngModel in the scope could have been named differently without affecting functionality.

Because the ngModel directive isn't aware of your "even-number" element, overriding and utilizing the $render() method to display values and $setViewValue() for updating values from UI are necessary. Only then will the $parsers and $formatters be triggered; otherwise, an ngModel scope variable behaves like any other.

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 separate chunks for individual files in Vue CLI3JsonPropertyTitleFileType

I am working on a project using vue-cli3 and need to separate out a specific file for chunking due to IIS requirements. Currently, webpack chunks files based on default settings and also automatically creates chunks from dynamic imports in vue-router. How ...

Having trouble using jQuery's .off() method to remove an event handler?

I'm facing an issue with my jQuery code where the .off('click') method doesn't seem to be working. I've tried removing the event binding from '.reveal-menu', but it's not working as expected. The initial animation wo ...

Assign a class to a DIV element depending on the ID of an object using Angular

I'm trying to dynamically add a class to a div based on the id of a field in an object. However, my code doesn't seem to be working as expected. Can someone help me debug this? <ng-container *ngFor="let item of cards"> <d ...

Full-Screen Popup Overlayaptic

Is it possible to create a popup that covers the entire browser window, including the search bar and other windows besides just the webpage area, for a very large interactive element? ...

Trouble with exporting and importing an Express application

Starting with a simple Express example of 'Hello World', I am looking to refactor the code into separate files for configuration and routing. var express = require('express'); var app = express(); app.get('/', function (req, ...

Proceed to the section with modal

My goal is to create a modal with 4 sections, each loading its content dynamically using the .load() function to the container on the right side. The challenge I'm facing is that I have a footer menu that triggers the modal to open, and I need it to ...

React: The received value for the prop type must be a function, but it was an object

I'm stuck trying to make sense of this error message, and my online search has hit a dead end. Any insights would be greatly appreciated! Attention: Prop type validation failed - expected prop type `leoInfo` to be a function from the `prop-types` pack ...

React CSS modules are a powerful tool for styling components in

Having some difficulty with css modules in react, I am unsure how to utilize react modules dynamically. import classnames from 'classnames' import styles from './hover.module.css /// /// const [flashElements,setFlashElements]=useState(elemen ...

Angular does not automatically add a class

My Angular function looks like this: $scope.show = function(el){ if($scope.steps[el] == true){ $scope.steps[el] = false; } else{ $scope.steps = []; $scope.steps[el] = true; } } When I trigger it by clicking on thi ...

Issue with AngularJs NgRoute

Encountering an issue with ngRoute on angularjs, version 1.6.9. Developed a simple route like "/test/:yourname" where "yourname" serves as a variable, however facing the following challenges: 1) Visiting the address "http://localhost:8080/test/rafael" re ...

Pass a bespoke object back to the GraphQL resolver

In my Node-Express backend server with GraphQL setup, I am working on providing a custom object as output for a GraphQL resolver. The usual Sequelize approach hasn't worked for me in this case, so I'm exploring new methods. const RootQueryType = ...

Resetting the CSS for an input field: a step-by-step guide

My situation involves having a global CSS style set for text type inputs, such as: input[type=text] { padding:10px; width:100px; //and many more } Now, I am incorporating a plugin called colorpicker into a specific div. This plugin generates some input e ...

Error: Unable to access the 'questionText' property as it is undefined

I encountered an error message stating that my "questionText" could not be read or is not defined. The issue seems to arise in the first code block where I use "questionText", while the intention is to drag it in the second code block. Is there a mistake ...

Developing a designated section of the code with specialized roles for administrators and vendors in React JavaScript

I am in the process of creating a dashboard for both admins and vendors. Is it feasible to structure the code so that if an admin builds one part, they have that option, and if a vendor builds another part, they have their own set of options? However, I w ...

Utilizing the power of both inheritance and modules in a NodeJS Tesla API project

I have been working on a NodeJS application using express and request modules where I am implementing the strategy design pattern to manage various HTTP requests efficiently throughout the application. My approach involves creating an Options base class an ...

Tips for labeling an object value in javascript

I have a collection of objects organized by index and I want to rearrange them while adding categories to the output. This is the initial collection: let vehicles = [{'make': 'audi', 'model': 'RS3', 'transmitio ...

Vuex state fails to reflect the updated data

I have integrated Vuex with axios to retrieve data from my backend. However, I am facing an issue where the state property userName is not updating within my Vue Single File Component(SFC). approot.js state const state = { userName: 'foo&apo ...

Creating Dynamic Page Titles with the MEAN stack: Leveraging the Power of Jade and Angular

I'm currently working on my MEAN stack app and I'm facing an issue with changing the page title (which is set in jade) based on the content that's being loaded on the page. Right now, it shows a generic page title for every page within the s ...

Strategies for addressing input range slider cross browser compatibility issues

I have encountered an issue with the slider track while using a customized range slider with CSS. For Mozilla, I utilized the selector for progress (-moz-range-progress) and for IE I used -ms-filler-lower and -ms-filler-upper. Although it works well for b ...

Walls that collide in three.js

I am currently developing a game using three.js and despite being new to this field, I have extensively studied collision documentation. To handle collisions between my boat (inside a cube) and the islands (contained in cubes), I implemented raycasting. He ...