Unknown developer
Unknown developer

Reputation: 5920

Angular directives way of defining

This example is from Angular docs. Could someone explain to me what exactly the following means?

<span my-dir="exp"></span>

I am talking about exp. Why do we need that? What does it represent?

Upvotes: 0

Views: 33

Answers (1)

lenilsondc
lenilsondc

Reputation: 9800

expr stands for expression, which in this case will be interpreted and evaluated by angularjs so that it can act like javascript from the directive declaration side.

This allow you to use it as an input param (aka args, aka input, etc) within your directive, it could be a function, an object a number, a string, a scope variable, etc. And from your directive side you are able to use this value for your directive purposes.

For example:

angular.module('app', [])
  .directive('helloWorld', function() {
    return {
      restrict: 'A',
      scope: {
        name: '@helloWorld'
      },
      template: 'Hello {{ name }}'
    };
  });
<div ng-app="app">
  <span hello-world="World"></span>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.js"></script>

Upvotes: 1

Related Questions