Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In angular.js, what does an ampersand in the directive mean?

Tags:

angularjs

In this code:

app.directive( 'myCustomDirective', function() {   return {     restrict: 'EA',     scope: { value: '=myCustomDirective', clickable: '&', editing: '=' },     template: '<span ng-click="handleClick()" ng-bind="value"></span>',     ... 

What does clickable: '&' denote?

When I do find Angular documentation, I cannot understand it. It seems to be written in a catch-22 code that can be understood only by people who already understand what is being explained.

For example, I found this video, which apparently explains the & thing, but he may as well have been speaking Mandarin.

like image 517
Cheeso Avatar asked Jun 23 '13 06:06

Cheeso


People also ask

What is =? In AngularJS?

Locals definition is a hash of local scope property to its source: = or =attr - set up bi-directional binding between a local scope property and the parent scope property of name defined via the value of the attr attribute.

What are directive in AngularJS?

AngularJS directives are extended HTML attributes with the prefix ng- . The ng-app directive initializes an AngularJS application. The ng-init directive initializes application data. The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.

What is directive element?

What are Directives? At a high level, directives are markers on a DOM element (such as an attribute, element name, comment or CSS class) that tell AngularJS's HTML compiler ( $compile ) to attach a specified behavior to that DOM element (e.g. via event listeners), or even to transform the DOM element and its children.


1 Answers

Update: The new directive API is here

There is some more explanations on the Understanding Scopes doc, and they provide this useful link.

When I wanted to understand this stuff, I made a Fiddle.

angular.module('scopePropertiesModule', [])   .directive('scopeProps', function(){     return {       restrict: 'C',       scope: {parameterTitle:'@',                bidirecTitle:'=',               delegateDisplay:'&'},       template: '<div>' +                   'Parameter title :<br>' +                    '<input ng-model="parameterTitle"> => {{parameterTitle}}<br>'+                   '<br>' +                   'Biderectional title :<br>' +                   '<input ng-model="bidirecTitle"> => {{bidirecTitle}}<br>' +                    '<br>' +                   'Delegate display :<br>' +                   '{{delegateDisplay()}}<br>' +                    '</div>'       } }); 

and the markup:

<div class="scopeProps"      parameter-title="{{title}}"       bidirec-title="title"      delegate-display="displayTitle()"></div> 

Feel free to play with it.

like image 78
Bastien Caudan Avatar answered Sep 25 '22 17:09

Bastien Caudan