Created
February 21, 2016 07:32
-
-
Save mid0111/195f5ad85f0aa56e36e6 to your computer and use it in GitHub Desktop.
AngularJS の broadcast pattern.
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
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 ); |
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
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