2014-04-03 36 views
0

共享$ scope.variable我有一個音頻播放器和正在使用該範圍滑塊 - 它工作得很好。 https://github.com/danielcrisp/angular-rangeslider/blob/master/angular.rangeSlider.js如何與孩子範圍

<li ng-controller="PlayerCtrl" style="display:inline-block;"> 
... 
<span range-slider min="0" max="100" ng-controller="PositionCtrl" model-max="position" pin-handle="min" filter="positionSilderText"> 
... 
</li> 

所以我做PlayerCtrl內一個新的控制器PositionCtrl像這樣:

angular.module('myApp.controllers', []).controller('PlayerCtrl', ['$scope','$timeout',function($scope,$timeout) { 
     $scope.audioTag=document.getElementById('audioPlayer');// an <audio/> tag 
     $scope.position = 0; 
     $scope.PositionCtrl = function ($scope) { 
       $scope.$watch('position', function() { 
        if (updatePlayPosition && $scope.audioTag.duration) { 
         $scope.audioTag.currentTime = $scope.audioTag.duration * $scope.position/100.0; 
        } 
        updatePlayPosition=true; 
       }); 
      }; 
      ... 
      $scope.ui=function() {//called by a $timeout 
       ... 
       var newpos = $scope.audioTag.duration; 
       $scope.position = setpos; 
      // $scope.$$childHead.position = setpos;// <<-- dont want to do this 
       .... 
      } 
    }); 

所以最初的UI()函數更新使用$ scope.position和所有的好我的滑塊POS。

我的問題是,一旦我拖動位置變量被在孩子創建的滑塊(PositionCtrl)範圍等手錶不再認爲在父(PlayerCtrl)範圍的變量。因此滑塊不再從ui()方法更新。

如果我取消分配的$$ childHead.position然後觀察者的作品就行了。但它不好,因爲我可能以後會增加更多的孩子,而AFAIK則無法從父母那裏訪問子範圍(不知道爲什麼)。

什麼是分享與兩個範圍的位置的正確方法是什麼?

回答

1

我認爲這同樣可以繼承的問題。查看這裏的繼承文章:https://github.com/angular/angular.js/wiki/Understanding-Scopes。希望這會有所幫助。我認爲這可能歸結爲把你想要觀察的東西放在一個對象內(而不是$ scope.position,做$ scope.someClevelModelName.position),並且觀察THAT屬性而不是外部範圍的位置屬性。

真希望這會有所幫助。

(張貼在這裏的SO)

0

,你可以隨時使用$scope.$parent,或者如果你不想,你可以在父範圍定義一個函數來更新所需的變量,然後從孩子範圍

app.controller('Parentctrl',function($scope) { 
    $scope.position= 1; 
    $scope.changePosition = function(p) { 
    $scope.position= p; 
    }; 
    $scope.PositionCtrl = function ($scope) { 
    $scope.$watch('position', function() { 
     if (updatePlayPosition && $scope.audioTag.duration) { 
     $scope.audioTag.currentTime = $scope.audioTag.duration * $scope.position/100.0; 
     } 
     updatePlayPosition=true; 
     $scope.changePosition(5); 
    }); 
    }; 
}); 

我裏面叫它希望這是你所需要的

+0

不大,用戶界面()方法是在父範圍,但手錶是在兒童範圍 - 所以這是行不通的。我需要'推倒'從父母到孩子的位置變化。不過謝謝你.. – siliconeagle

+1

你是否試圖讓這個職位成爲全球?它有點不好意思,但它做的不是$ scope.position,而是$ scope. $ root.position,那麼它將全局訪問 –

+0

仍然沒有看到這將如何改變事情,因爲問題是,當我拖動滑塊的位置變量在子範圍中創建 - 所以問題會持續存在。 – siliconeagle