0

I was going to use Plunker to assist me in testing a directive, but first I just wanted to create one to test plunker was working, so I put in some sample code. Guess what, basic directives are not working and I have no idea why.

My directives:

app.directive('attributeDirective', function() {
  return {
    restrict: 'A',
    link: function(scope, iElement, iAttrs) {

      iElement.bind('click', function() {
        console.log('clicked attributeDirective');
      });
      iElement.bind('mouseover', function() {
        iElement.css('cursor', 'pointer');
      });
    }
  };
});

app.directive('elementDirective', function() {
  return {
    restrict: 'E',
    replace: true,
    template: '<h2>this is elementDirective</h2>',
    link: function(scope, iElement, iAttrs) {

      iElement.bind('click', function() {
        console.log('clicked elementDirective');
      });
      iElement.bind('mouseover', function() {
        iElement.css('cursor', 'pointer');
      });
    }
  };
});

My html:

<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>

<h2 attributeDirective>Here is my attribute directive</h2>

<elementDirective></elementDirective>

</body>

http://plnkr.co/edit/H9vPhV

Gargaroz
  • 313
  • 9
  • 28
Ingó Vals
  • 4,788
  • 14
  • 65
  • 113

4 Answers4

3

while calling a directive in html you should replacecamelcase in directives name like this,

<element-directive></element-directive> and not as it is,

<elementDirective></elementDirective>

like you did.

Hope this helps!!!

PLUNKER

see through the custom directives here

Alhuck
  • 1,029
  • 8
  • 11
  • Please look at this [question](https://stackoverflow.com/questions/31001134/how-to-create-network-graph-or-hierarchical-tree-using-visjs-in-angularjs) in here my [plunker](http://plnkr.co/edit/SKfNdsw9g9aCG6wZsQNZ?p=preview) is not working. Please help. –  Jun 23 '15 at 12:06
2

You should use

<h2 attribute-directive>Here is my attribute directive</h2>

See http://plnkr.co/edit/2aGGDRw6SdYNc1joSVI1?p=preview

Navaneet
  • 1,367
  • 1
  • 19
  • 44
2

Common problem - you can't use camel case in your HTML element declaration.

Try <element-directive></element-directive>

Jake Wright
  • 199
  • 2
0

Use restrict: 'A' in your directive to refer to attribute. Use restrict: 'E' in your directive to refer to element.

Find the plunkr: "http://plnkr.co/edit/b1cf6l?p=preview"

Also call your directive using:

<h2 attribute-directive>Here is my attribute directive</h2>   
<element-directive></element-directive>
Nikhil Batra
  • 3,118
  • 14
  • 19