0

比方說,我的祖父母組件/指令的範圍變量聲明如下:$scope.g = { // methods here };拖放角1.x的指令/組件和家長的範圍

在我的每個嵌套組件/指令的控制器,我指的是那個變量如$scope.$parent.$parent.g,然後調用g的某些功能,如$scope.fromNgClick = function() { $scope.$parent.$parent.g.run(); };。這很好(儘管我想要一個更好的方式來引用祖先,比如別名,而不是$ parent鏈)。

當我從它的祖父母組件中將一個組件拖動到另一個祖父母組件中時(相同的$scope.fromNgClick = function() { $scope.$parent.$parent.g.run(); };仍然指向原始範圍,而不是像我需要的那樣)。因此,單擊相同的ng-clickable元素仍會觸發前一個祖父母範圍的run()方法。

這一切都有道理;但是,有沒有辦法讓範圍指向新的刪除位置祖父母範圍呢?

謝謝!

編輯
的標記將類似於以下內容,其中<g-directive>被視爲祖父母,因爲它在它的模板使用transclude,最終包裹孩子的指令:

<g-directive> 
    <child-directive></child-directive> 
    <another-child-directive></another-child-directive> 
    <yet-another-child-directive></yet-another-child-directive> 
</g-directive> 
<g-directive> 
    <c-child-directive></c-child-directive> 
    <c-another-child-directive></c-another-child-directive> 
    <c-yet-another-child-directive></c-yet-another-child-directive> 
</g-directive> 

這是對的原因關於子指令/組件的$scope.$parent.$parent.g

子組件可以被拖動,然後放入另一個<g-directive>,但它的控制器仍然指向它的原始祖父母(原始的<g-directive>的控制器範圍變量)。我希望它指向新的祖父母被放入,基本上重置它的範圍到新放置的範圍。

+0

如果你想看看這個: http://stackoverflow.com/questions/11252780/whats-the-correct-way-to-communicate-between-controllers-in-angularjs 不知道你爲什麼需要從當前範圍調用很多父母。 – batmaniac7

+0

@maddog我認爲這對其他一些情況很有幫助,但我不知道如何解決孩子對新祖父母的範圍引用。 –

+0

你可以'發射'和'廣播'來呼叫其他控制器。並使用共享服務或'$ rootScope'變量來共享數據。 – batmaniac7

回答

0
<g-directive some-attr=1> 
<child-directive></child-directive some-attr=1> 
<another-child-directive></another-child-directive some-attr=1> 
<yet-another-child-directive></yet-another-child-directive some-attr=1> 
</g-directive> 

<g-directive some-attr=2> 
<child-directive></child-directive some-attr=2> 
<another-child-directive></another-child-directive some-attr=2> 
<yet-another-child-directive></yet-another-child-directive some-attr=2> 
</g-directive> 

每個人都可以有不同的偵聽broadcastemit

如果指令通過ng-repeat重複,那麼$index可以是該屬性。 希望這有助於。

+0

昨天感謝您的詳細信息。我設法改變了包裝組件的結構,以解決我原來分配給嵌套組件的一些功能,而這對我的用例來說一直工作正常。 –

+0

很酷。要知道。 – batmaniac7