Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs not updating variables

I want to display/update the calculation answer directly without the need of aditional buttons. Or do i need a function and button to apply the changes?

<div ng-app="myApp" ng-controller="myCtrl">

A_Number: <input type="number" step="1" ng-model="A_Number"><br> 

Display (A_Number): {{A_Number}}
<br>
square: {{square}} 
</div>

controller:

<script> 
var app = angular.module('myApp', []); 
app.controller('myCtrl', function($scope) {

$scope.square = $scope.A_Number * $scope.A_Number; 
}); 
</script>
like image 782
Medera Avatar asked Jun 08 '26 18:06

Medera


1 Answers

Make square as a method & use that method in interpolation directive, so that will evaluate on each digest.

Markup

square: {{square()}} 

Code

$scope.square = function(){
    //made error free
    if(!isNaN($scope.A_Number))
      return $scope.A_Number * $scope.A_Number;
    return 0;
};
like image 63
Pankaj Parkar Avatar answered Jun 10 '26 10:06

Pankaj Parkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!