require

angular.module('myApp',[])
    //require可以将其他指令传递给自己
//directivename:通过驼峰法的命名指定了控制器应该带有哪一条指令,默认会从同一个元素上的指令
    //^directivename:在父级查找指令
    //?directivename:表示指令是可选的,如果找不到,不需要抛出异常
 .directive('bookList',function () {
        return {
            restrict:'ECAN',
            controller:function ($scope) {
                console.log($scope);
               $scope.books=[
                   {name:'php'},
                   {name:'javascript'},
                   {name:'java'}
               ];
               this.addBook=function () {
                   alert(1);
                   $scope.$apply(function () {
                      $scope.books.push({
                          name:'Angularjs'
                      })
                   })
               }
            },
            controllerAs:'bookListController',
            template:'<div><ul><li ng-repeat="book in books">{{book.name}}</li></ul><book-add></book-add></div>',
            replace:true
        }
 })
    .directive('bookAdd',function () {
        return{
            restrict:'ECAN',
            require:'^bookList',
            template:'<button type="button">添加</button>',
            replace:true,
            link:function (scope,iElement,iAttrs,bookListController) {
                console.log(scope)
                //iElement.on('click',bookListController.addBook);
            }
        }

    })
    .controller('firstController',['$scope',function ($scope) {

    }]);

  

posted @ 2017-10-18 17:51  zhujhhxx  阅读(90)  评论(0编辑  收藏  举报