2014-09-04 57 views
0

給定兩個示波器 - x,y - 是否有內置函數,如果x是y的祖先,則返回true?是否有內置的方法來知道範圍是否是另一個範圍的孩子?

(我可以明顯地從y中穿越到$rootScope使用$parent一路上比較$id

編輯:

在這期間我使用的是這樣的:

function isChildScope(parentScope, childScope) { 
    while (childScope) { 
     if (parentScope.$id === childScope.$id) { 
      return true; 
     } 
     childScope = childScope.$parent; 
    } 
    return false; 
}; 
+0

可能,這可能回答這個問題:http://stackoverflow.com/a/13428220/1059101 – Jai 2014-09-04 12:19:51

+0

@Jai - 這個答案沒有任何與我的問題......我並不想訪問子範圍,只要找出一個範圍是否是另一個範圍的祖先。 – seldary 2014-09-04 12:23:53

+0

所以你想在代碼或角鉻擴展名爲batarang肯定會幫助你。 – Jai 2014-09-04 12:25:39

回答

1

來源中$scope沒有內置方法,所以我懷疑它是否在其他地方。你可能會像你說的那樣比較$id或簡單地用x.$parent === y來檢查。

0

如果您正在尋找清楚地識別controller一個$scope屬於我建議控制器

<!-- In Your Binding --> 
<div ng-controller="MyCtrl as ctrl"> 
    <span>{{ctrl.foo}}</span> 
</div> 

<!-- In Your Controller --> 
app.controller('MyCtrl', function() { 
    this.foo = "Hello World!"; 
}); 

這句法將雙重約束的controller但會使$scope你的工作有明確界定。


這是嵌套控制器的一個很好的例子,它顯示瞭如何提高可讀性。

<div ng-controller="MainCtrl as main"> 
    {{ main.title }} 
    <div ng-controller="AnotherCtrl as another"> 
    Scope title: {{ another.title }} 
    Parent title: {{ main.title }} 
    <div ng-controller="YetAnotherCtrl as yet"> 
     Scope title: {{ yet.title }} 
     Parent title: {{ another.title }} 
     Parent parent title: {{ main.title }} 
    </div> 
    </div> 
</div> 
+1

謝謝,但這不是我要找的。 – seldary 2014-09-04 12:22:03

+0

_if x是y _...的祖先是這個答案嗎? – Jai 2014-09-04 12:22:31

+0

對不起@seldary,這真的是我有這個最好的答案。我建議這樣做的原因是因爲它迫使你將值設置在適當的範圍內,而不是依賴於父範圍。 – Malkus 2014-09-04 12:26:36

相關問題