2017-09-22 7 views
0

このディレクティブ定義オブジェクト - (restrict)と混乱します。 私は2つの関数を作成しました。最初はrestrictで、もう1つはrestrictなしです。"restrict"と "restrict"を伴わない角ディレクティブ

このコードを実行すると、両方のディレクティブが同じ結果を返しました。restrict

app.directive 'helloWorld', -> 
    return { 

    restrict: 'AE' 
    link: (scope, elem, attrs) -> 
     console.log "HELLO WORLD" 

    } 

restrictなし:

app.directive 'helloWorld', -> 
    return { 

    link: (scope, elem, attrs) -> 
     console.log "HELLO WORLD" 

    } 

は、誰かがここで何が起こっているかを教えてもらえますか? PS:私は新しい角度です。あなたがこれで私を助けることができれば、本当に感謝します。

答えて

0

ディレクティブは、それはディレクティブの定義のrestrictプロパティでサポートしている4つのマッチングタイプのを指定することができますオブジェクト。

The defaultはEAである。すなわち、restrictが指定されていない場合。必要に応じてこれらの制限は、すべて組み合わせることができ

'A' - only matches attribute name 
'E' - only matches element name 
'C' - only matches class name 
'M' - only matches the comment 

: - 試合のいずれかの属性または要素またはクラス名 または

「AEC」

制限するオプションが一般的に設定されていますECA - 注文は関係ありません)

source - Angularjs docs

app.directive 'helloWorld', -> 
    return 
    restrict: 'AE' 
    link: (scope, elem, attrs) -> 
     console.log "HELLO WORLD" 

app.directive 'helloWorld', -> 
    return 
     link: (scope, elem, attrs) -> 
      console.log "HELLO WORLD" 

は同じであり、差がありません。あなただけrestrict Eをお持ちの場合はどちらも、

<helloWorld> ... </helloWorld> 

または

<ANY helloWorld> ... </ANY> 

言うように使用することができます。あなたはキャメルケースの方法であなたのディレクティブに名前を付けるならば、あなたはあなたのhtmlコードでそれを使用するときに-でそれを交換する必要があります。そして、ディレクティブの機能は、ほんの少しの先端

<helloWorld> ... </helloWorld> 

<ANY helloWorld> ... </ANY> // wont work since the directive is bound only to element 
+0

"デフォルトはEAです(制限が指定されていない場合)。"ありがとう@Sibi Raj。今私にはそれがはっきりしています。 –

+0

ようこそ、ありがとうございました。 –

2

Restrictは、ディレクティブが一致する必要のある要素のタイプを指し、ディレクティブの戻り値には何も影響しません。必要に応じてangular docs:

'A' - only matches attribute name 
'E' - only matches element name 
'C' - only matches class name 
'M' - only matches comment 

からこれらの制限は、すべて組み合わせることができます

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

+1

OPのディレクティブが 'restrict: 'AE'の有無にかかわらず同じ理由は、ディレクティブのデフォルトの制限が' 'EA ''であるからです。 –

0

にのみ適用されますこのような

あなたは restrict

E値を設定
<hello-world></hello-world> 

として

<ANY hello-world></ANY> 

Aの値を設定すると、

関連する問題