Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass string straight to angular directive?

Tags:

angularjs

I'm trying to understand how to work with directives in Angular. In the examples on angularjs.org, values are set in the scope in JavaScript, and then that scope is referenced when matching a directive.

Template:

  <my-customer info="naomi"></my-customer>

Scope:

  .controller('Ctrl', function($scope) {
    $scope.naomi = { name: 'Cat', address: '1600 Amphitheatre' };
    $scope.igor = { name: 'Dog', address: '123 Somewhere' };
  })

Template:

Name: {{customerInfo.name}} Address: {{customerInfo.address}}

I would like to pass the attribute, straight from the template. So in this example I would like to write a template that outputs:

Name: Naomi

Without going through a scope.

like image 876
Himmators Avatar asked Sep 10 '26 23:09

Himmators


1 Answers

You can do that using the attrs argument of the linking function.

module.directive('name', function () {
  return {
    link: function (scope, element, attrs) {
      console.log(attrs.info);
      // <- 'naomi'
    }
  }
});

Then in markup:

<name info='naomi'></name>

See a live example

like image 174
bevacqua Avatar answered Sep 13 '26 17:09

bevacqua