Cétia Cétia - 2 years ago 54
Javascript Question

What's the use of the "attrs" parameter in an angular directive link function

Considering this directive :

.directive('myDirective', function(httpRequestTracker) {
return {
restrict: 'EA',
scope: {
myvar: "=",
link: function($scope, elem, attrs) {
console.log($scope.myvar, attrs.myvar);

and this JS console output :

> undefined undefined // if no attribute

> value value // if attribute value = "value"

What is the use of the
parameter of the
function ? What is the difference with $scope ?


Answer Source

attrs is just a raw list of attributes on a directive
scope is more sophisticated, you can use three different operators to populate it with values:

  • = evaluates expression in HTML, and may contain objects
  • @ interprets value passed as string. always.
  • & gives you access to functions defined on parent scope
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download