1

I made some AngularJS directives in the past week and they all work but this one just won't work and I don't know what I am doing wrong..

This is the directive I'm talking about:

app.directive('idleCheck', [function () {
  return {
      restrict: 'I',
      link: function (scope, elem, attrs) {
          ifvisible.setIdleDuration(5);
          ifvisible.on("idle", function () {
              var div = document.getElementById('fullscreenWrap');
              div.style.cursor = 'none';
              stream.pause();
          });

          ifvisible.on("wakeup", function () {
              var div = document.getElementById('fullscreenWrap');
              div.style.cursor = 'auto';
              stream.resume();
          });
      }
  }
}]);

This is my HTML code where I call the directive:

<div id="fullscreenWrap" idle-check>
  ...
</div>

Do you see anything wrong in the code ? Or do you know why it isn't working ?

3
  • 1
    change restrict to 'A' Commented Sep 25, 2015 at 12:23
  • 1
    What exactly is the error is giving? Are all the directives declared the same? There is no restrict: I. There is only A - only matches attribute name, E - only matches element name, C - only matches class name. You can however give a combination of the three. Commented Sep 25, 2015 at 12:24
  • 1
    Directive only works with restrict type A,E and C. Change restrict: 'I',to restrict: 'A/C/E' Commented Sep 25, 2015 at 12:25

3 Answers 3

3

You need to change restrict field to 'A'.

The restrict option is typically set to:

'A' - only matches attribute name 'E' - only matches element name 'C' - only matches class name These restrictions can all be combined as needed:

'AEC' - matches either attribute or element or class name

Angular directive

Sign up to request clarification or add additional context in comments.

Comments

2

The restrict options available are: 'E','A','C','M'

One of EACM restricts the directive to a specific directive declaration style.

You can even use Multiple restrictions on same directive restrict: 'AC'

If you don't restrict any, the defaults (elements and attributes) are used.

E - Element name (default): <my-directive></my-directive>

A - Attribute (default): <div my-directive="exp"></div>

C - Class: <div class="my-directive: exp;"></div>

M - coMment: <!-- directive: my-directive exp -->

For Example:

ng-if is restricted to 'A'. so it can be used only as attribute, you can't use as comment or element

Here's the angularjs code for ngIf

var ngIfDirective = ['$animate', function($animate) {
  return {
    transclude: 'element',
    priority: 600,
    terminal: true,
    restrict: 'A',       // --> This means restricting to Attribute

Comments

1

Without knowing what error it is actually giving your issue is most likely your directive declaration.

There is no restrict: I. Angular only supports three values for this:

A - only matches attribute name E - only matches element name C - only matches class name

You can but any combination of the three though to support multiple cases.

Docs: https://docs.angularjs.org/guide/directive#template-expanding-directive It states the info at the bottom of the template expanding directive section.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.