Why my directives don't work in Plunker?

110 views Asked by At

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

4

There are 4 answers

1
Alhuck On BEST ANSWER

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

0
Nikhil Batra On

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>
0
Navaneet On

You should use

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

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

0
Jake Wright On

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

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