Search code examples
angularjsangularjs-scope

Calling a function on scope with Expression won't return anything


Given following simple controller:

(function () {
    angular.module('store').controller('CartCtrl', ['$scope', 
            function ($scope) {

        $scope.cartSumFunction = function () {
            return 678;
        };

        $scope.cartSum = 678;

    }]);

})();

If I try to output cartSumFunction with an expression, it fails (nothing is printed):

<div data-ng-controller="CartCtrl">
    <p>{{ cartSum }}</p> <!-- Works! -->
    <p>{{ cartSumFunction }}</p> <!-- Doesn't work, nothing there! -->
</div>

Why does the expression calling a function with a return value not work?


Solution

  • try this:

    <div data-ng-controller="CartCtrl">
        <p>{{ cartSum }}</p>
        <p>{{ cartSumFunction() }}</p>
    </div>