ng-click inside指令不起作用

时间:2016-05-16 10:34:43

标签: javascript angularjs

这是我的HTML代码:

<section ng-controller="GalleryController as gallery">
    <nav footer-directive></nav>
</section>  

控制器:

angular
    .module('myapp')  
    .controller('GalleryController', GalleryController);

  GalleryController.$inject = ['GalleryService'];

  function GalleryController(GalleryService){
     var self=this;
     self.current_index=0;

     self.leftArrow=function(){
         if (self.photos.length!==0){
           if (self.current_index===0) {
               self.current_index=self.photos.length-1;
           } else {
               self.current_index--;
           }
           self.selected_photo=self.photos[self.current_index];
         }
     };

     self.rightArrow=function(){
         if (self.photos.length!==0){
           if (self.current_index===self.photos.length-1) {
               self.current_index=0;
           } else {
               self.current_index++;
           }
           self.selected_photo=self.photos[self.current_index];
         }
     };

     self.searchResults=function(){
         return GalleryService.getPhotos(self.search_tag,self.page).then(function() {
            self.photos = GalleryService.photos;
            self.selected_photo=self.photos[0];
            var total=GalleryService.total_photos_number;
            self.total_pages=total%15===0 ? total/15 : (total-total%15)/15+1;
         });
     };

     self.searchResults();
  } 

和指令footerDirective

angular
    .module('myapp')  
    .directive('footerDirective', footerDirective);
function footerDirective(){
     return { 
         link: function(scope,elements,attributes){
                var new_div=document.createElement("div"); 
                new_div.setAttribute("ng-click", "gallery.leftArrow()");
                new_div.innerHTML=content;
                new_div.className=cname;
                elements[0].appendChild(new_div); 

         }
     }
  } 

为什么ng-click不适用于new_div?

1 个答案:

答案 0 :(得分:1)

使用$compile应该有效

$compile(new_div)(scope)