2016-06-29 1 views
-2

の違いは何ですか?継承は

親スコープからの継承にどのような影響がありますか?偽

は、このディレクティブは、独自のスコープを取得しないことを意味:私は最初の角1.1で

適用範囲を始めたとき

+0

問題の内容を説明してください。 –

+2

http://www.undefinednull.com/2014/02/11/mastering-the-scope-of-a-directive-in-angularjs/ – Ajay

答えて

1

雅が、これは本当にあまりにも私を混乱しました。だから、$スコープはありません。$ newが呼び出されます。つまり、ディレクティブがアクセスするスコープは親スコープです。これをスコープを共有するものと考えてください。

<div ng-controller="CtrlA"> 
    <div ng-controller="CtrlB"> 
     <my-directive></my-directive> 
    </div> 
</div> 

// In this example, the directive code's scope chain looks like this 
// $rootScope --> CtrlA scope --> CtrlB scope 
// So if our my-directive calls scope.doSomething() 
// It looks in CtrlB first to see if that exists, 
// because my-directive doesn't have its own scope 
// Then it checks CtrlA, and finally $rootScope 
// Standard prototypical inheritance 

範囲:真

手段このディレクティブは、現在のスコープを継承し、独自のスコープを、望んでいます。 $ scope。$ newが呼び出され、現在のスコープが親として設定されます。スコープチェーンの仕組みです。

<div ng-controller="CtrlA"> 
    <div ng-controller="CtrlB"> 
     <my-directive></my-directive> 
    </div> 
</div> 

// In this example, the directive code's scope chain looks like this 
// $rootScope --> CtrlA scope --> CtrlB scope --> my-directive scope 
// So if our my-directive calls scope.doSomething() 
// It looks in my-directive first to see if that exists 
// Then it checks CtrlB, CtrlA, and finally $rootScope 
// Standard prototypical inheritance 

範囲:{}これは、単離されたスコープと呼ばれる

。隔離されたスコープは、継承チェーンなしで独自のスコープです。この指令は隔離されています。それは渡されたパラメータ(通常はディレクティブ要素の属性)にしかアクセスできず、コールバックを介してメッセージを送信する唯一の方法(属性としても設定されていますが、これらは面白い呼び出し方法があります)

<div ng-controller="CtrlA"> 
    <div ng-controller="CtrlB"> 
     <my-directive></my-directive> 
    </div> 
</div> 

// In this example, the directive code's scope chain looks like this 
// my-directive scope 
// So if our my-directive calls scope.doSomething() 
// It only looks in my-directive to see if that exists