你可以像一個功能設置的第一個參數watchExpression:
app.controller('MainCtrl', function($scope, voipService) {
$scope.speakerVolume = voipService.speakerVolume;
$scope.$watch("speakerVolume", function (newValue) {
voipService.speakerVolume = newValue;
});
});
app.controller('SecondCtrl', function($scope, voipService) {
$scope.$watch(function() { return voipService.speakerVolume }, function (newValue) {
$scope.speakerVolume = newValue;
});
});
app.service('voipService', function() {
this.speakerVolume = 2;
});
有兩個數據綁定:
<p ng-controller="MainCtrl">
<input ng-model="speakerVolume" />
{{speakerVolume}}
</p>
<p ng-controller="SecondCtrl">{{speakerVolume}}</p>
Plunker
謝謝。這是一個很好的解決方案,但它不適合我的問題,因爲我需要它雙向綁定,這裏是一種方法。如果我做兩個手錶,我最終會得到循環。 – liorafar