The function is not being called by ngModelController $parsers in Angular version 1.4.7

I'm encountering an issue with ngModel.NgModelController.$parsers. Let me explain with an example: I have created a directive for an input field. Whenever I change the value in this input in the HTML, it should call my specified function.

The registered function is supposed to always change whatever is written in the input field to the character 'A', but modify the model.

html file:

<div ng-app="myApp">
  <form>
    <input ng-model="myval1" somedir/>
  </form>
</div>

and js file:

var myApp = angular.module('myApp', []);
myApp.directive("somedir", function(){
  return {
    restrict:'A',
    require: '?ngModel',
    link: function(scope, element, attrs, ngModelController) {
      ngModelController.$parsers.unshift(function(val){
        console.log("i'm here");
        ngModelController.$viewValue ="A";
        ngModelController.$render();
        return val;
      });

    }
  }
});

I have prepared a jsfiddle for testing:

angular 1.2.9 : http://jsfiddle.net/hwzxfon3/1/

angular 1.4.7 : http://jsfiddle.net/372re41m/1/

In angular 1.2.9, it works correctly. However, in angular 1.4.7, there seems to be a problem. Here's how to reproduce it: focus on the input field and press the same button (e.g. '1') three times. The first two times, it should show 'A' in the input field and in the browser console it should display i'm here. However, on the third press, it displays 'A' followed by the pushed character (e.g. 'A1') and doesn't log i'm here in the browser console, indicating that the registered function wasn't called.

Could you please let me know if there's a bug in my code (and suggest changes) or if it's a bug in Angular (in which case, I will report it to the Angular team)? I have tested it in the latest versions of Chrome and Safari on OS X (10.10.5). If it works fine for you, please mention that as well.

Answer №1

After reaching out to the Angular team, I received a comprehensive response which can be found here.

To summarize, it is recommended to use the following code in similar situations:

ngModelController.$setViewValue("A");

instead of:

ngModelController.$viewValue ="A";

A functioning jsfiddle example for Angular 1.4.7 can be accessed here: http://jsfiddle.net/455qdd6p/1/

This information was essential for resolving an issue in my Angular module found at https://github.com/uhlryk/angular-dynamic-number

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

Create an Android application using Node.js

While developing my mobile application, I am utilizing html5 with node.js to create a chat box for users connected on the same wireless network. However, I encounter an issue when coding on my desktop using node.js software. How can I overcome this challen ...

Why is there nothing showing up on the screen?

Just diving into Three.js and might've messed up somewhere. I'm trying to generate a geometry by using coordinates and surface data from a .k file. I checked the geometry.vertices and geometry.faces, they seem fine. I even played around with the ...

How can a command in a test be executed that is located within a specific "section" in Nightwatch?

I've been utilizing nightwatch for my test automation. Within my page object, I have a "section" containing various commands. However, when attempting to call these commands in the test script, I encountered an error stating "section is not a function ...

A guide to organizing elements in Javascript to calculate the Cartesian product in Javascript

I encountered a situation where I have an object structured like this: [ {attributeGroupId:2, attributeId: 11, name: 'Diamond'}, {attributeGroupId:1, attributeId: 9, name: '916'}, {attributeGroupId:1, attributeId: 1, name ...

What is the process for passing a component as a parameter to a function within a different component?

Within my scenario, I have a series of components '1,2,3,...' imported into another primary component 'A'. Within component 'A', certain operations are performed and a filtered component is then returned from the list of compo ...

Tips on using constructor functions and the new keyword in Typescript

This is a demonstration taken from the MDN documentation showcasing the usage of the new keyword function Car(make, model, year) { this.make = make; this.model = model; this.year = year; } const car1 = new Car('Eagle', 'Talon TSi&apos ...

Developing maintenance logic in Angular to control subsequent API requests

In our Angular 9 application, we have various components, some of which have parent-child relationships while others are independent. We begin by making an initial API call that returns a true or false flag value. Depending on this value, we decide whether ...

Dynamic headers with $save in AngularJS $resource

I have been attempting to include dynamic headers in a $resource as shown below: angular.module('app') .factory('API', function ($resource, API_URL) { return { event: function(userId){ return $resource(API_URL + '/event/ ...

The URL does not contain the complete hash

I'm currently working on a gallery using Jquery Elastislide. var imageIndex = 0; if (window.location.hash) { var imageIndexStr = window.location.hash.replace('#',''); // removing # imageIndex = parseInt(imageIndexStr, 0) ...

The hyperlink functionality is disabled because Javascript is set to return false

JS Fiddle Example When using the 'FOO' and 'BOO' items in the navigation bar to open dropdown boxes, I have implemented a code that closes them when a click event occurs outside. This code has been working fine as shown below: $(docum ...

Interactive Javascript dropdown helper on change

I am currently experimenting with a JavaScript onchange method, as I am relatively new to coding. My goal is that when I select "ID Type," the input text should change to "passport," and when I select "South African ID," the input should switch to "South ...

The React component fails to inherit any props that are passed to it when it is rendered using a logical operator

I'm facing an issue with a component that doesn't seem to receive any props when I use a logical operator in conjunction with it. Oddly enough, if I render the component without the conditional statement, everything works fine. But as soon as I a ...

Django enables anonymous Ajax requests to reach a Generic View

Here is the current progress I have made: from django.views.generic import View from django.views.decorators.csrf import csrf_exempt class ConfigurationView(View): @csrf_exempt def dispatch(self, *args, **kwargs): return super(Configurati ...

Having trouble resolving "react-native-screens" from "node_modules eact-navigation-stacklibmoduleviewsStackViewStackViewCard.js"? Here's how to fix it

Here is the command I used for setting up react app routes: npm i react-native-router-flux --save After restarting npm with "npm start," I encountered this error message: Unable to resolve "react-native-screens" from "node_modules\react-navigation- ...

Browser Compatibility in Three.js

Could someone assist me with activating webgl on Internet Explorer version 8? I am able to use Firefox and Chrome without any issues. Your help would be greatly appreciated. ...

The themes showcased in the Bespoke.js documentation and demo exhibit unique designs

Recently, I stumbled upon an incredible HTML5 framework for presentations. For more information, you can check out the documentation here. You can also view a demo of the framework by visiting this link. However, I was disappointed to find that the caro ...

Replicate the styling of CSS class A and apply it to class B

Let's take a look at some code: <button id="test" class="ui-state-hover" value="Button"> In Context: I'm utilizing the JQuery UI CSS class ui-state-hover on an HTML button to ensure it always appears as if it's being hovered over. H ...

Still Facing the 'appendChild' Error Even After Defining it

Looking for assistance in creating new elements to display information on a Discord bot list I'm currently developing. var btn = document.createElement("BUTTON"); btn.innerHTML = "Try It"; document.body.appendChild(btn); ...

Unable to set a default value for a select control using a custom directive

I am facing a simple scenario with a custom directive in AngularJS. In this case, if there is only one item in the drop-down menu, it should be selected by default; otherwise, it should function as a normal drop-down list. For this situation, I attempted ...

What is the best method for implementing a Twitch <script> tag within Vue.js?

In an effort to replicate the functionality I achieved in Angular here, I am now attempting to do so within Vue.JS (2.6+). My goal is to utilize the Twitch API for embedding a Stream, which currently only offers usage through inline HTML: <script src= ...