AngularJS Tutorial - Pass expression to a custom directives








The following code shows how to pass expression to a custom directives.

Example


<!doctype html>
<html ng-app="MyApp">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
    <script>
var app = angular.module("MyApp", []);
<!--from ww  w. j  a v a2  s.com-->
app.directive("yourWidgetExpr", function() {
  var linkFunction = function(scope, element, attributes) {
    scope.text = scope.fn({ count: 5 });
  };

  return {
    restrict: "E",
    template: "<p>{{text}}</p>",
    link: linkFunction,
    scope: {
      fn: "&fn"
    }
  };
});    
    </script>
  </head>
  <body>
     <your-widget-expr fn="count = count + 1"></your-widget-expr>

  </body>
</html>

The code above is rendered as follows: