Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make AngularJS rerun a function every time the data it uses changes?

Tags:

angularjs

With Knockout, I can say

<html>
    <head>
        <script type="text/javascript" src="knockout-2.1.0.js"></script>
    </head>
    <body>
        <input type="text" data-bind="value: a"></input> +
        <input type="text" data-bind="value: b"></input> =
        <span data-bind="text: result"></span>
        <script type="text/javascript">
            function ExampleViewModel() {
                this.a = ko.observable(5);
                this.b = ko.observable(6);
                this.result = ko.computed(function() {
                    return parseInt(this.a()) + parseInt(this.b());
                }, this);
            }

            ko.applyBindings(new ExampleViewModel());
        </script>
    </body>
</html>

and result will be recalculated every time a and b change. How can I get AngularJS to do this for me? I have tried

<html ng-app>
    <head>
        <script type="text/javascript" src="angular-1.0.1.min.js"></script>
        <script type="text/javascript">
            function ExampleCtrl($scope) {
                $scope.a = 5;
                $scope.b = 6;
                $scope.result = function() {
                    return this.a + this.b
                };
            }

</script>
    </head>
    <body ng-controller="ExampleCtrl">
        <input type="text" value="{{ a }}"></input> +
        <input type="text" value="{{ b }}"></input> =
        {{ result() }}
    </body>
</html>

After a little more reading, I found ng-change:

<html ng-app>
    <head>
        <script type="text/javascript" src="angular-1.0.1.min.js"></script>
        <script type="text/javascript">
            function ExampleCtrl($scope) {
                $scope.a = 5;
                $scope.b = 6;
                $scope.result = function() {
                    return parseInt($scope.a) + parseInt($scope.b)
                };
            }

</script>
    </head>
    <body ng-controller="ExampleCtrl">
        <input type="text" ng-model="a" ng-change="result()"></input> +
        <input type="text" ng-model="b" ng-change="result()"></input> =
        {{ result() }}
    </body>
</html>

But that requires me to keep track of the fact that changing a or b changes result(), is there any automatic way of detecting this?

like image 346
Chas. Owens Avatar asked Aug 08 '12 18:08

Chas. Owens


1 Answers

Your result() function will be re-evaluated whenever the model changes when binding via ng-model in your inputs like this:

<input type="text" ng-model="a"></input>

instead of:

<input type="text" value="{{ a }}"></input>
like image 154
Gloopy Avatar answered Sep 20 '22 12:09

Gloopy