angularjs controller, service, directive 的demo

<!DOCTYPE html>
<html>
    <head>
         <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="bootstrap.min.css">
        <script src="jquery.min.js"></script>
        <script src="angular.min.js"></script>
        <script src="bootstrap.min.js"></script>
        <script type="text/javascript">
            angular.module('myapp', [])
            .controller('BookController', ['$scope', 'AddBookService', function($scope, AddBookService) {
                $scope.books = AddBookService.books;
                $scope.$on('update-books', function(event) {
                    $scope.books = AddBookService.books;

                    //在AngularJS上下文之外的任何地方修改了model,那么你就需要通过手动调用$apply()来通知AngularJS
                    $scope.$apply();
                });
            }])
            .factory('AddBookService', ['$rootScope', function($rootScope) {
                tmp = {
                    books : [
                        {'name' : 'The Adventures of Tom Sawyer', 'author': 'Mark Twain'},
                        {'name' : 'Robinson Crusoe', 'author': 'Daniel Defoe'},
                        {'name' : 'A Ghost Story', 'author': 'Mark Twain'}
                    ],
                    addBook : function(book) {
                        tmp.books.push(book);
                        $rootScope.$broadcast('update-books');
                    }
                };
                return tmp;
            }])
            .directive('addBookBtn', ['AddBookService', function(AddBookService) {
                return {
                    restrict: 'E',
                    replace: false,
                    template: '<input type="text" class="input-small" ng-model="bookName" ' +
                              'placeholder="BookName"><br/><br/>' +
                              '<input type="text" class="input-small" ng-model="bookAuthor" ' +
                              'placeholder="BookAuthor"><br/><br/>' +
                              '<button class="btn btn-mini btn-primary">Add Book</button>',
                    link: function(scope, elem,attrs) {
                        scope.bookName = '';
                        scope.bookAuthor = '';
                        //elem = add-book-btn
                        elem.children('button').bind('click', function() {
                            if (scope.bookName != '' && scope.bookAuthor != '') {
                                AddBookService.addBook({'name': scope.bookName, 'author':scope.bookAuthor});
                                scope.bookName = '';
                                scope.bookAuthor = '';
                                scope.$apply();
                            }
                        });
                    }
                };
            }]);
        </script>
        <style type="text/css">
        </style>
    </head>
    <body ng-app="myapp">
       <div ng-controller="BookController">
            <table class="table table-bordered table-hover table-condensed">
                <caption>Book</caption>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Author</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="book in books">
                        <td>{{book.name}}</td>
                        <td>{{book.author}}</td>
                    </tr>
                </tbody>
            </table>
       </div>
       <add-book-btn></add-book-btn>
    </body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值