1. Common obstacles in the functionality of data binding2. Constraints

Working on a basic controller to perform some calculations, which is a simplified version of a more complex project. The issue I'm facing is that the result displayed in the HTML gets recalculated every time there's a change, but when calculating within the $scope as a variable, it doesn't update. Refer to the markup comments for details.

Any insights on what could be causing this behavior?

Markup

<body ng-app="myApp"> 
<div ng-controller="mainController">
  <h3> Numbers </h3> 
  <label> a </label>
  <input type="number" ng-model="numbers.a"/>

  <label> b </label> 
  <input type="number" ng-model="numbers.b">
<br>
<br>
  <span> Result : {{result}} {{numbers.a*numbers.b}} </span> // the first one does not update, but the second does. 

  <h3> Nums </h3> 

    <label> a </label>
  <input type="number" ng-model="a1">

  <label> b</label> 
  <input type="number" ng-model="b1">

<br>  
  Result2:  {{result2}} {{a1+b1}}
<ul> 
  <li ng-repeat=" i in cool"> {{i}} </li>
  </ul>
  </div>
</body> 

Javascript:

angular.module('myApp',[])

.controller('mainController', ['$scope', function($scope) {
  $scope.numbers = {a:11, b:10}; 
  $scope.a1 = 5; 
  $scope.b1 = 7; 
  $scope.result = $scope.numbers.a*$scope.numbers.b; 
  $scope.result2 = $scope.a1 +$scope.b1 ;

  $scope.cool = [$scope.result + $scope.result2, 
               $scope.result - $scope.result2]
}]);

http://codepen.io/Tallyb/pen/rVdebm

Answer №1

The variable result does not update because the mainController function is only evaluated once, which happens when Angular first interprets the HTML and discovers the ng-controller="mainController" expression.

To automatically update the result, you need to set up watch listeners in the controller as shown below:

angular.module('myApp',[])

.controller('mainController', ['$scope', function($scope) {
  // ...
  $scope.$watch('[numbers.a, numbers.b]', function () {
    $scope.result = $scope.numbers.a * $scope.numbers.b;
  });

}]);

An expression like {{numbers.a*numbers.b}} will update automatically as Angular sets up watch listeners for it. The expression syntax in HTML is just syntactic sugar - Angular internally uses $watch functions for every expression found in the HTML.

Refer to the $watch documentation for more details.

I personally avoid using the $watch syntax mentioned above as it can make the controller bulky. Instead, I prefer calling a function from the HTML like this:

{{ calculateResult() }}

In the controller, define the function as follows:

angular.module('myApp',[])

.controller('mainController', ['$scope', function($scope) {
  // ...
  $scope.calculateResult = function () {
    return $scope.numbers.a * $scope.numbers.b;
  };

}]);

Side note: If performance is a concern and the calculateResult() function is slow, sticking to the initial version might be preferable.

Answer №2

When the controller is initialized, the calculations are performed only at that time when the corresponding view is shown. To incorporate these calculations, you need to place them within functions (no $watch needed):

$scope.result = function() {
  return $scope.numbers.a * $scope.numbers.b;
}
$scope.result2 = function() {
  return $scope.a1 + $scope.b1;
}
$scope.cool = function() {
  return [
    $scope.result() + $scope.result2(),
    $scope.result() - $scope.result2()
  ];
}

Then, make sure to refer to them in your view:

<span> Result : {{result()}} {{numbers.a*numbers.b}} </span>

Also:

Result2:  {{result2()}} {{a1+b1}}

Lastly:

<li ng-repeat=" i in cool(result, result2)"> {{i}} </li>

For more information, visit: http://codepen.io/anon/pen/vORXpg

Answer №3

To ensure the correct calculation of the result when numbers are changed, it is essential to have a watch in place. Otherwise, the value of result will be computed only once.

var app = angular.module('my-app', [], function() {})

app.controller('mainController', ['$scope',
  function($scope) {
    $scope.numbers = {
      a: 11,
      b: 10
    };
    $scope.a1 = 5;
    $scope.b1 = 7;

    // Recalculate result if 'a' or 'b' change
    $scope.$watch('[numbers.a, numbers.b]', function() {
        $scope.result = $scope.numbers.a * $scope.numbers.b;
      })
      
    // Recalculate result2 if 'a1' or 'b1' change
    $scope.$watch('[a1, b1]', function() {
      $scope.result2 = $scope.a1 + $scope.b1;
    })

    // Recalculate 'cool' if result or result2 change
    $scope.$watch('[result, result2]', function() {
      $scope.cool = [$scope.result + $scope.result2,
        $scope.result - $scope.result2
      ]
    })
  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="my-app">
  <div ng-controller="mainController">
    <h3> Numbers </h3> 
    <label>a</label>
    <input type="number" ng-model="numbers.a" />

    <label>b</label>
    <input type="number" ng-model="numbers.b" />
    <br/>
    <br/>
    <span> Result : {{result}} {{numbers.a*numbers.b}} </span> // the first one does not update, but the second does.

    <h3> Nums </h3> 

    <label>a</label>
    <input type="number" ng-model="a1" />

    <label>b</label>
    <input type="number" ng-model="b1" />

    <br/>Result2: {{result2}} {{a1+b1}}
    <ul>
      <li ng-repeat=" i in cool">{{i}}</li>
    </ul>
  </div>
</div>

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

Imitate a HTTP request

Currently, I am working on developing a front-end application using Angular (although not crucial to this question). I have a service set up that currently supplies hard-coded JSON data. import { Injectable } from '@angular/core'; import { Obser ...

What is the best way to access form data in React using a React.FormEvent<HTMLFormElement>?

I am looking for a way to retrieve the values entered in a <form> element when a user submits the form. I want to avoid using an onChange listener and storing the values in the state. Is there a different approach I can take? login.tsx ... interfa ...

Using Angular to set an empty value for an anchor tag with hash or javascript

As we dive into learning Angular.js, our lead JS developer has suggested a specific approach when mocking up links that do not have a designated page yet. Instead of using the traditional <a href="#">, he recommends using <a href="javascript://"&g ...

JavaScript - Unexpected fluctuations in variable values

After studying Japanese language, I decided to try my hand at experimenting with JavaScript by creating a simple FlashCard game for a project. The game generates an array of random numbers, fills the divs with 6 possible choices using jQuery, randomly sele ...

Tips on expanding the dimensions and incorporating more members in a radar graph's Chartjs tag

I need to make some adjustments to the font size and color in a radar chart. Specifically, I want to change the name on the side of each data point. I have already tried adjusting the legend labels using the following code: options={{ ...

Prevent content from occupying unnecessary space below a sticky div

When the "link" on the sticky header is clicked in this scenario, how can I ensure that the linked content item (#mypara) appears below the sticky div rather than directly underneath it where it may be hidden? $(document).ready(function() { $(window ...

Send a text value as an argument to a callback function

This code snippet showcases how I am fetching user details: $scope.userDetails = function(userId, type) { ajax.get(orginalData.blockData.USER_BASIC_DETAILS + userId, $scope.getUserDetailsCallBack) } $scope.getUserDetailsCallBack = function(result) { ...

Moving information from one controller to another, or the process of converting a controller into a service

Is there a way for me to transfer information from one controller to another? Or can I create a service from a controller? Specifically, I am looking to retrieve coordinates and store them in an object along with other variables. When I try to inject depen ...

Enable automatic playback of HTML5 video with the sound on

I want to add an autoplay video with sound on my website, but I'm running into issues with newer browsers like Chrome, Mozilla, and Safari blocking autoplay if the video doesn't have a 'muted' attribute. Is there a clever HTML or Javas ...

Testing controls in AngularJS is an essential part of verifying the

Just diving into the world of Angular and wanting to write some basic unit tests for my controllers, here is what I have so far. app.js: 'use strict'; // Define the main module along with its dependencies angular.module('Prototype', ...

Incorporating Error Management in Controller and Service: A Step-by-Step Guide

Take a look at the structure of my angular application outlined below: Within my 'firm.html' page, there is a button that triggers the code snippet provided. Controller The controller initiates a Service function. The use of generationInProgre ...

Attaching a modal to an entity

I am currently working on binding a Knockout (KO) viewmodel to a Bootstrap modal, but it seems like I am overlooking a step to direct KO to fill in the input fields. Below is the current setup: The template for the modal: <script type="text/html" id= ...

Tips for looping through each cell in a column of a DataTable to verify its content

I have a table generated using the jquery DataTables API. One of the columns displays word frequencies for each word in the table. If a frequency is less than 40, I want to change that cell to display "unranked" instead of the actual number. How can I ite ...

Redux: One container for many components

I am new to React and Redux, and I am currently working on a project where I am unsure of the best practices and technical solutions. I am following Dan Abramov's definitions of "smart" and "dumb" components which can be found here. The component I a ...

What is the best way to include bootstrap using webpack?

I am currently building a webapp using Typescript and webpack. I have been able to successfully import some modules by including them in my webpack.config.js file as shown below. However, no matter how many times I attempt it, I cannot seem to import the b ...

When NuxtImg is utilized, the image will rotate 90 degrees with Nuxt3 NuxtImg

Recently, I have encountered an issue when using NuxtImg where my images appear rotated by 90°. This problem specifically arises with vertical mobile images that are read from supabase and displayed. Interestingly, the images look fine before uploading th ...

Netbeans lacks compatibility with JavaScript

Recently, I've been encountering code errors in my .js files while attempting to edit JavaScript using Netbeans 6.9. Even after upgrading to Netbeans 7.01 and enabling the JAVA plugin, the issue persists. I'm unable to create a new JavaScript tem ...

Comparing the use of input parameters to pass information in node.js versus the use

I'm grappling with the concept of when to inject a response into a function or call a function and retrieve something from it. Specifically in Node.js. Do functions in Node.js actually return data, or is it primarily about passing arguments and utili ...

Unable to create a clickable button within a CSS3DObject using Three.js

How can I create an interactive button on a CSS3DObject within a cube made up of 6 sides? The button is located on the yellow side, but I'm facing issues with clicking on it. Whenever I attempt to click on the button, the event.target always points to ...

Adding a new element with Jquery when a dropdown option is selected

What is causing this issue to not function properly? <script> $(document).ready(function(){ $('#custom_field option').click(function(){ $('#custom_field_input').append('<tr><td></td> ...