Skip to content Skip to sidebar Skip to footer

Angularjs : Watching Service Properties

Take the following plunk as an example: http://plnkr.co/edit/vKFevXhhSprzFvesc6bG?p=preview var app = angular.module('plunker', []); app.service('SomeService', ['$rootScope', func

Solution 1:

When you assign the value of SomeService.value to your scope variable, you are creating a copy of the variable which is a distinct object from the value inside SomeService. By adding the watch expression you were simply keeping the two variables (the one in the scope and the one in SomeService) synchronised.

The easiest way to go about this is not to copy the value, but create a reference to the service itself. So in MainCtrl

$scope.someService = SomeService;

and in your html

Value: {{someService.value}}

this way you are actually binding to the value inside SomeService.

Post a Comment for "Angularjs : Watching Service Properties"