When working with directives, it is often helpful to create a sibling scope for the directive that is separate from the normal hierarchical scope. This allows multiple directives to exist at the same level without interfering with each other's properties. AngularJS provides three different ways to bind properties in your directive's scope definition, as outlined in the directive documentation.
scope: {
urlParamAttr: '@URL_PARAM',
urlParamEval: '=URL_PARAM',
urlParamFn:'&URL_PARAM'
}
- @ / @name: Binds an attribute's value directly to the directive scope. For example, the property
urlParamAttr
will be bound to the HTML attribute URL_PARAM
, resulting in a string value like some_url.jpg
.
- = / =name: Evaluates the attribute in the context of the defining scope and binds it to a directive property. If the attribute is a valid JavaScript expression or reference, it will be evaluated accordingly.
- & / &name: Treats the attribute as a function that can be called by the directive. Useful for passing functions between scopes.
In order to utilize these bindings in your template, you can define them in the directive scope like so:
.directive('customImage', function() {
return {
replace: true,
scope: {
urlParam: '@URL_PARAM',
figParam: '@FIG_PARAM'
},
template: '<div> <img src="{{urlParam}}"/> {{figParam}} </div>'
};
});