2016-06-09 154 views
-1

我有以下指令Angularjs。訪問變量指令範圍

var chartDir = function() { 
    return { 
     scope: { 
     stat: '=' 
     }, 
     link: function (scope, element, attr) { 
     console.log(scope); 
     return; 
     } 
} 

HTML

<svg chart stat="stat"></svg> 

因此,在這種情況下,我可以在控制檯中看到該範圍內有一個名爲stat屬性(一個數組,不是空的!)

這裏是截圖...

enter image description here

但是,如果我做console.log(scope.stat)那麼我只得到undefined。我在其他圖表中有相同的指令,它工作正常。但在這裏我不明白爲什麼我不能去scope.stat

UPD訪問: 更多的HTML

<div class="chart-container container" ng-show="statLoaded"> 
    <div class="text col"> 
    <div class="item" ng-repeat="item in stat | limitTo: middleIndex"> 
     <div class="color"> 
     <div class="sample" color-sample color="item.color"></div> 
     </div> 
     <div class="amount"> 
     {{ item.amount }} 
     </div> 
     <div class="text"> 
     {{ item.text }} 
     </div> 
    </div> 
    </div> 
    <div class="col"> 
    <svg chart stat="stat"></svg> 
    </div> 
... 

所以我看到的所有項目,並指令「顏色樣本」的作品好。所以'stat'被加載。那麼,爲什麼'顏色樣本'指令起作用,'圖表'不起作用?

+0

貌似對指令的父'stat'屬性執行你的指令之前尚未確定。當你看到控制檯窗口時,已經有足夠的時間讓'stat'屬性被設置爲 –

+0

。我瞭解控制檯。但是,我怎麼能通過統計來宣傳。接近這個塊,我有另一個'stat',它的加載非常好,所以'stat'被加載並存在(在控制器中,應該傳遞給這個derective)。 我也添加了範圍$ watch(scope.stat,function(){.. to derective,但它仍然是undefined。 我怎樣才能渲染我的感覺,當我得到'統計'加載? – Filosoff

回答

0

解決方案是

scope.$watch(attr.stat, function() { 

,而不是

scope.$watch(scope.stat, function() {