共享$ 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則無法從父母那裏訪問子範圍(不知道爲什麼)。
什麼是分享與兩個範圍的位置的正確方法是什麼?
不大,用戶界面()方法是在父範圍,但手錶是在兒童範圍 - 所以這是行不通的。我需要'推倒'從父母到孩子的位置變化。不過謝謝你.. – siliconeagle
你是否試圖讓這個職位成爲全球?它有點不好意思,但它做的不是$ scope.position,而是$ scope. $ root.position,那麼它將全局訪問 –
仍然沒有看到這將如何改變事情,因爲問題是,當我拖動滑塊的位置變量在子範圍中創建 - 所以問題會持續存在。 – siliconeagle