Skip to content

Instantly share code, notes, and snippets.

@mid0111
Created February 21, 2016 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mid0111/195f5ad85f0aa56e36e6 to your computer and use it in GitHub Desktop.
Save mid0111/195f5ad85f0aa56e36e6 to your computer and use it in GitHub Desktop.
AngularJS の broadcast pattern.
var ctrl = [ '$scope', 'Book', function( scope, Book ) {
scope.$on( 'books.update', function( event ) {
scope.books = Book.books;
});
scope.books = Book.books;
}];
module.controller( "books.list", ctrl );
module.service( 'Book', [ '$rootScope', function( $rootScope ) {
var service = {
books: [
{ title: "Magician", author: "Raymond E. Feist" },
{ title: "The Hobbit", author: "J.R.R Tolkien" }
],
addBook: function ( book ) {
service.books.push( book );
$rootScope.$broadcast( 'books.update' );
}
}
return service;
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment