Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs - Pass argument to directive

Im wondering if there is a way to pass an argument to a directive?

What I want to do is append a directive from the controller like this:

$scope.title = "title"; $scope.title2 = "title2";  angular.element(document.getElementById('wrapper')).append('<directive_name></directive_name>'); 

Is it possible to pass an argument at the same time so the content of my directive template could be linked to one scope or another?

here is the directive:

app.directive("directive_name", function(){     return {         restrict:'E',         transclude:true,         template:'<div class="title"><h2>{{title}}</h3></div>',         replace:true     }; }) 

What if I want to use the same directive but with $scope.title2?

like image 819
SKYnine Avatar asked Oct 16 '14 16:10

SKYnine


People also ask

Which directive initializes an AngularJS application?

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 custom directive in AngularJS?

Custom directives are used in AngularJS to extend the functionality of HTML. Custom directives are defined using "directive" function. A custom directive simply replaces the element for which it is activated.

What is scope in AngularJS directive?

Scope in a Directive Well, all of the directives have a scope associated with them. This scope object is used for accessing the variables and functions defined in the AngularJS controllers, and the controller and link functions of the directive.


1 Answers

You can pass arguments to your custom directive as you do with the builtin Angular-directives - by specifying an attribute on the directive-element:

angular.element(document.getElementById('wrapper'))        .append('<directive-name title="title2"></directive-name>'); 

What you need to do is define the scope (including the argument(s)/parameter(s)) in the factory function of your directive. In below example the directive takes a title-parameter. You can then use it, for example in the template, using the regular Angular-way: {{title}}

app.directive('directiveName', function(){    return {       restrict:'E',       scope: {          title: '@'       },       template:'<div class="title"><h2>{{title}}</h2></div>'    }; }); 

Depending on how/what you want to bind, you have different options:

  • = is two-way binding
  • @ simply reads the value (one-way binding)
  • & is used to bind functions

In some cases you may want use an "external" name which differs from the "internal" name. With external I mean the attribute name on the directive-element and with internal I mean the name of the variable which is used within the directive's scope.

For example if we look at above directive, you might not want to specify another, additional attribute for the title, even though you internally want to work with a title-property. Instead you want to use your directive as follows:

<directive-name="title2"></directive-name> 

This can be achieved by specifying a name behind the above mentioned option in the scope definition:

scope: {     title: '@directiveName' } 

Please also note following things:

  • The HTML5-specification says that custom attributes (this is basically what is all over the place in Angular applications) should be prefixed with data-. Angular supports this by stripping the data--prefix from any attributes. So in above example you could specify the attribute on the element (data-title="title2") and internally everything would be the same.
  • Attributes on elements are always in the form of <div data-my-attribute="..." /> while in code (e.g. properties on scope object) they are in the form of myAttribute. I lost lots of time before I realized this.
  • For another approach to exchanging/sharing data between different Angular components (controllers, directives), you might want to have a look at services or directive controllers.
  • You can find more information on the Angular homepage (directives)
like image 56
PzYon Avatar answered Oct 14 '22 20:10

PzYon