2015-09-25 91 views
0

我在过去一周取得了一些AngularJS指令和他们所有的工作,但这个是行不通的,我不知道我做错了..AngularJS指令限制

这是指导我米聊一下:

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(); 
      }); 
     } 
    } 
}]); 

这是我的HTML代码,我调用指令:

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

你看到什么错误代码? 或者你知道它为什么不起作用吗?

+1

变化限制在“A” – Marcio

+1

恰恰是错误的给予什么?所有的指令都是一致的吗?没有'限制:我'。只有“A” - 只匹配属性名称,“E” - 只匹配元素名称,“C” - 只匹配类名称。但是,您可以将三者结合起来。 – ste2425

+1

指令仅适用于限制类型A,E和C.更改限制:'I',限制:'A/C/E' – Ved

回答

2

您需要将restrict字段更改为'A'。

的限制选项通常设置为:

“A” - 只匹配属性名称“E” - 只匹配元素名称“C” - 只匹配类名这些限制可以全部被组合为需要:

'AEC' - 匹配任一属性或元素或类名

Angular directive

1

不知道它实际上给你的问题是什么错误很可能是你的指令声明。

没有restrict: I。角仅支持这三个值:

A - 只匹配属性名称 E - 只匹配元素名称 C - 只匹配类名

你可以,但三,虽然任意组合,以支持多个案件。

Docs:https://docs.angularjs.org/guide/directive#template-expanding-directive 它声明在template expanding directive部分底部的信息。

1

可用restrict选项有:'E''A''C''M'

一个EACM限制指令到特定的指令,声明风格。

你甚至可以使用多在同一指令restrict: 'AC'

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

E - 元素名称(默认值):<my-directive></my-directive>

A - 属性(默认):<div my-directive="exp"></div>

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

M - 注释:<!-- directive: my-directive exp -->

对于实施例:

ng-if限于'A'。因此它可以只用作属性,则不能作为评价或元素使用

下面是ngIf

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