Last active
December 23, 2015 14:45
-
-
Save Bolza/4e75c9890a07973ad4c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//`first` reads the data from the service | |
Controller first (myService) -> | |
this.firstValue = myService.get().firstValue //using a getter so there's no direct reference | |
myService.onChange(function(newData) { | |
this.firstValue = newData.firstValue | |
}) | |
//`second` sets the data inside the service | |
Controller second (myService) -> | |
myService.set('firstValue', this.secondValue); | |
// 'myService' can handle the event subscription | |
Service myService ($http, $rootScope) -> | |
function get -> | |
$http.get().then(fireEvent) | |
// using the $rootScope (if we wont have many of these istances) | |
function onChange(cb) -> | |
$rootScope.$on('myService:change', cb) | |
function fireEvent (data)-> | |
$rootScope.$emit('myService:change', data) | |
// or we can use a custom pub/sub class that doesnt touch the rootScope | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment