Skip to content

Commit

Permalink
Use rx observables to track changes to the service value.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamGraber committed Apr 25, 2016
1 parent 821ef22 commit b90f54a
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions source/watchInService/watchInService.ts
@@ -1,9 +1,24 @@
import * as angular from 'angular';
import * as Rx from 'rx';

export const moduleName: string = 'watchInService';

class WatchedService {
watchedValue: number;
private _watchedValue: number;
watchedValueObservable: Rx.Subject<number>;

get watchedValue(): number {
return this._watchedValue;
}

set watchedValue(value: number) {
this._watchedValue = value;
this.watchedValueObservable.onNext(value);
}

constructor() {
this.watchedValueObservable = new Rx.Subject();
}
}

class ServiceProviderController {
Expand All @@ -24,9 +39,9 @@ function serviceProvider(): angular.IDirective {
class WatchInServiceController {
doubledValue: number;

static $inject: string[] = ['$scope', 'watchedService'];
constructor($scope: angular.IScope, public watchedService: WatchedService) {
$scope.$watch('controller.watchedService.watchedValue', (value: number): void => {
static $inject: string[] = ['watchedService'];
constructor(public watchedService: WatchedService) {
watchedService.watchedValueObservable.subscribe((value: number): void => {
this.doubledValue = value * 2;
});
}
Expand Down

0 comments on commit b90f54a

Please sign in to comment.