In Angular JS, the event count for ng-model is consistently set to 11

After creating a sample of all the Angular Js events, I noticed that when the page is initially loaded, the default value of the model textbox is always showing as 11. I am curious to understand why this default value is set to 11.

Here is an excerpt from my HTML code: Examples on Click events

        <input ng-blur="blur=blur+1" />
        Blur Events : {{blur}}<br/>

        <input ng-click="click=click+1" />
        Click Events : {{click}}<br/>

        <input ng-dblclick="dblclick=dblclick+1" />
        Double click Events : {{dblclick}}<br/>

        <input ng-copy="copy=copy+1" />
        Copy Events : {{copy}}<br/>

        <input ng-paste="paste=paste+1" />
        Paste Events : {{paste}}<br/>

        <input ng-cut="cut=cut+1" />
        Cut Events : {{cut}}<br/>

        <input ng-focus="focus=focus+1" />
        Focus Events : {{focus}}<br/>

        <input ng-model="model1=model1+1" />
        Model Events : {{model1}}<br/>

        <input ng-change="change=change+1" />
        Change Events : {{change}}<br/>

        <input ng-keydown="keydown($event)" />
        Keydown Events : {{kdkey}}<br/>

        <input ng-mouseenter="mouseenter=mouseenter+1" />
        Mouseenter Events : {{mouseenter}}<br/>

        <input ng-mouseleave="mouseleave=mouseleave+1" />
        Mouseleave Events : {{mouseleave}}<br/>

    </div>
</body>

</html>

My Javascript code snippet appears as follows:

    var app5 = angular.module('MyApp5', []);
    app5.controller('eventCtrl', function ($scope) {
        $scope.blur = 0;
        $scope.click = 0;
        $scope.dblclick = 0;
        $scope.copy = 0;
        $scope.paste = 0;
        $scope.cut = 0;
        $scope.model = 0;
        $scope.change = 0;
        $scope.mouseenter = 0;
        $scope.mouseleave = 0;
        $scope.keydown = function(e) {
            $scope.kdkey = String.fromCharCode(e.keyCode);
        };
    });

I am wondering if the reason the default value for model is set to 11 could be related to it being a directive (ng-model) and possibly interacting with other event directives?

Despite changing the values and trying different approaches such as using model1, I still find that the default value remains at 11. Any insights on this behavior would be greatly appreciated.

EDIT

You can also view a similar example on JSFiddle: here.

I have not been able to successfully resolve this issue. If you have any suggestions or solutions, please share them with me.

Answer №1

Angular may be interpreting your variable as a string rather than an integer

console.log(1 + "1") // => 11
console.log("1" + "1") // => 11
console.log(1 + 1) // => 2

To remedy this, consider using the parseInt() method:

console.log(parseInt("1") + 1) // => 2

Answer №2

The occurrence of 11 ng-directives in your script is the reason behind this. Each time other elements are updated, the ng-model also gets updated.

Answer №3

The example provided on jsfiddle leaves much to be desired.

For an improved version, check out this corrected jsfidlle.

When it comes to angular events, here's a better approach:
angular.module('MyApp5', [])
  .controller('eventCtrl', function($scope) {
    $scope.blur = 0;
    $scope.click = 0;
    $scope.dblclick = 0;
    $scope.copy = 0;
    $scope.paste = 0;
    $scope.cut = 0;
    $scope.model = 0;
    $scope.change = 0;
    $scope.mouseenter = 0;
    $scope.mouseleave = 0;
    $scope.keydown = function(e) {
      $scope.kdkey = String.fromCharCode(e.keyCode);
    };
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="MyApp5">
  <div ng-controller="eventCtrl">


    <input ng-blur="blur=blur+1" /> Blur Events : {{blur}}
    <br/>

    <input ng-click="click=click+1" /> Click Events: {{click}}
    <br/>

    <input ng-dblclick="dblclick=dblclick+1" /> Double click Events: {{dblclick}}
    <br/>

    <input ng-copy="copy=copy+1" /> Copy Events: {{copy}}
    <br/>

    <input ng-paste="paste=paste+1" /> Paste Events: {{paste}}
    <br/>

    <input ng-cut="cut=cut+1" /> Cut Events: {{cut}}
    <br/>

    <input ng-focus="focus=focus+1" /> Focus Events: {{focus}}
    <br/>

    <input ng-model="model1" /> Model Events: {{model1}}
    <br/>

    <input ng-model="model1"  ng-change="change=change+1" /> Change Events: {{change}}
    <br/>

    <input ng-keydown="keydown($event)" /> Keydown Events: {{kdkey}}
    <br/>

    <input ng-mouseenter="mouseenter=mouseenter+1" /> Mouseenter Events: {{mouseenter}}
    <br/>

    <input ng-mouseleave="mouseleave=mouseleave+1" /> Mouseleave Events: {{mouseleave}}
    <br/>

  </div>
</body>

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

Could there possibly exist a counterpart to .cloneNode?

I'm currently working on a recipe website submission form, and I've successfully implemented code that allows me to add more ingredients dynamically. addIngredientsBtn.addEventListener('click', function(){ let newIngredients = ingre ...

The AutoComplete component in React Material UI does not automatically assign the initialValue

I've encountered an issue with my form.js component. In this component, I have two states: update and create. Within the component, there's an AutoComplete component that works perfectly fine in the create state (data creation works as intended). ...

Show a Popup Modal after Submitting a GET Request

How can I display the SQL code returned from various input parameters as a Modal? I am struggling to have the POST request made from inputs show up in the Modal when pressing the submit button. I know Ajax is usually involved in this process, but how do I ...

Stop modal from closing in the presence of an error

My approach involves using a generic method where, upon adding a food item, a modal window with a form opens for the user to input their details. However, since backend validation for duplicate items can only be retrieved after the API call completes. I w ...

What could be causing my function to execute thrice?

I cannot seem to figure out why my tag cloud is causing the required function to run multiple times instead of just once when I click on a tag. This issue persists whether using jQuery or plain JavaScript. What am I missing? The code I have is very simple, ...

Tips for dynamically adjusting an iframe's content size as the browser window is resized

Currently, I am in the process of building a website that will showcase a location on a map (not Google Maps). To achieve this, I have utilized an iframe to contain the map and my goal is for the map to adjust its width based on the width of the browser wi ...

What is the best method for ensuring that my JavaScript tracking script has been properly installed on the customer's website?

We provide a SAAS analytics application that functions similarly to Hotjar and Google Analytics. To track user data, our customers must integrate our JavaScript code into their websites. How can we confirm if the script has been successfully integrated in ...

Creating an array of input data for component, implementing data formatting in TypeScript for Angular or React applications

Here is an example of the input for the component. When this input is passed to the component, the output displays correctly. const data = [ ['Rank', 'Player', 'Player', 'Player'], ['1& ...

EJS selectively displaying certain elements from an object

This particular issue has been baffling me. I have been passing an object into an ejs template and when I output that object, everything appears as expected: { _id: 5504a5e7ff67ac473dd7655c, code: 'GB', name: 'United Kingdom', slug: & ...

Is there a way to determine the color of an element when it is in a hover state?

I recently started using the Chosen plugin and noticed that the color for the :hover on the <li> elements is a bright blue. I want to change it to a bold red color instead. https://i.stack.imgur.com/mcdHY.png After inspecting it with the Chrome too ...

Waypoints unable to display - Google Maps API issue

I am attempting to retrieve the waypoints from an AXIOS call and utilize the response to populate the city into a waypts array. Unfortunately, when I try to include the waypoints in the route, the map only shows the route from the starting point to the des ...

Nodemon is failing to automatically re-run the changes I've made in my local codebase

Recently, I developed a basic node function that simply outputs "hello world." When I ran the script using the command line node myfirst.js, it successfully displayed the message in the browser. Then, I decided to install nodemon so that it would re-exec ...

Exploring the power of AngularJS directives in combination with ng-click

When working with a controller function, I have encountered an issue while calling a REST API that returns an array. To display this data in a HTML table, I am using ng-repeat along with a custom directive like so: <player id="transaction.id_player_to" ...

Using Vue to display a Div inside a TD element of a table does not result in a reactive behavior

I am encountering an issue with a table where the last column contains a div with three options (View, Edit, and Delete). This submenu is initially hidden, but when clicking on the options button in the last column of the table, the array used to control i ...

Utilizing Django's URL dispatcher dynamically in JavaScript

Im attempting to implement a dynamic url dispatcher as follows: "{% url 'url_name' 'variable' %}" where variable is generated dynamically in my javascript. My goal is to redirect to another page when the value of a <selec ...

In Angular, the process of duplicating an array by value within a foreach function is not

I have been attempting to duplicate an array within another array and make modifications as needed. this.question?.labels.forEach((element) => { element["options"] = [...this.question?.options]; // I've tried json.stringify() as wel ...

Vue.js: Select a different div within the Vue object instead of the one that is bound

I am currently utilizing Vue and Leaflet to showcase polygons (zones) on a map and exhibit relevant information (messages) upon clicking on specific polygons. The div responsible for rendering these messages has the unique id "#messagearea" and is connec ...

The proper method for waiting for a link to be clicked using Selenium

On my web page, I have the following HTML: <a id="show_more" class="friends_more" onclick="Friends.showMore(-1)" style="display: block;"> <span class="_label">Show More Friends</ ...

Using *ngFor to iterate through a nested collection in an Angular 2 application

I'm currently working on a challenge involving drilling down to iterate over an array within another collection of arrays within an Angular 2 application. To start off, I have set up my component to subscribe to an observable in the ngOnInit lifecycle ...

Learning the ins and outs of Node.js: Creating a client to connect to a Node.js server and receive broadcast messages

In implementing my nodeJS Server, everything seems to be running smoothly. However, now I am looking to create a client that can receive messages from the server and trigger specific JavaScript functions based on those messages. The process involves: Us ...