Having trouble using ngChange to update model

1.9k Views Asked by At

I just started learning the basics of Angular. I'm attempting to making an annual salary converter, just for fun. I'm having difficulty with my monthly ng-model updating when the yearly model is changed by the user. The fields are input tags. Here is the code

    <!doctype html>
<html ng-app="salaryApp">
    <head>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
        <div class="container" ng-controller="converter">
            <h1>Salary converter</h1>
            <div class="form-group">
                <label>Annual Salary</label>
                <input type="number" class="form-control" placeholder="0" ng-model="yearly" ng-change="reCalculate()" >
                <br>
                <label>Monthly Salary</label>
                <input type="number" class="form-control" placeholder="0" ng-model="monthly" disabled>
            </div>

        </div>
        <!--<div ng-controller="converter">
            Write some text in textbox:
            <input type="text">

            <h1>Hello {{ yearly }}</h1>
            <h1>Hello {{ monthly }}</h1>
        </div>-->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
        <!--<script src="salaryConverter.js"></script>-->
        <script type="text/javascript">
            var app = angular.module('salaryApp', []);



app.controller('converter', function($scope) {
  $scope.yearly = 80000;
  console.log("log1");
  $scope.monthly = $scope.yearly / 12;
  console.log("log2");

    function reCalculate() {
        console.log("function was run");
        return $scope.yearly /12.00;

  }

});
</script>

    </body>
</html>

Here is the plnkr http://plnkr.co/edit/26y0JRR7iVcrLOBlm7D2?p=preview

4

There are 4 best solutions below

2
On BEST ANSWER

Youe need to use it as scope property. Here :

function reCalculate() {
        console.log("function was run");
        return $scope.yearly /12.00;

  }

should be

  $scope.reCalculate = function () {
        console.log("function was run");
        $scope.monthly=$scope.yearly /12.00;//Don't return, you neet to assign

  }
0
On

Define function as in $scopeto be called from view and assign value to monthly instead of returning.

$scope.reCalculate = function () {
    console.log("function was run");
    $scope.monthly = $scope.yearly / 12.00;
}

DEMO

0
On

You need to add the reCalculate method to your scope:

app.controller('converter', function($scope) {
  $scope.yearly = 80000;
  $scope.reCalculate = reCalculate; <--- THIS LINE
  console.log("log1");
  $scope.monthly = $scope.yearly / 12;
  console.log("log2");

    function reCalculate() {
        console.log("function was run");
        return $scope.yearly /12.00;

  }

});

You could also add it directly:

$scope.reCalculate = function()...

But I recommend to follow some style guides on how to write your controllers: https://github.com/johnpapa/angular-styleguide

0
On
app.controller('converter', function($scope) {
  $scope.yearly = 80000;
  console.log("log1");
  $scope.monthly = $scope.yearly / 12;
  console.log("log2");

    $scope.reCalculate = function () {
        console.log("function was run");
        $scope.monthly = $scope.yearly / 12;

  }

});

Here is your plunker working