AngularJS Tutorial - Bind to function in scope in expression








The following code shows how to bind to function in scope in expression.

Example


<!doctype html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
</head><!--from  w ww  .  j a  v  a2s  .  co m-->
<body ng-app="myModule">
  <div ng-controller="MyCtrl">
    <input type="text" placeholder="Name" ng-model="name"/>
    <p>{{greeting()}}</p>
</div>
<script type='text/javascript'>
var myModule = angular.module('myModule', [])
    .controller('MyCtrl', ['$scope', function ($scope) {

        $scope.greeting = function(){
            return $scope.$eval('"Hello "+name');
        };
    }]);
</script>
</body>
</html>

The code above is rendered as follows: