2016-05-16 82 views
1

這裏是我的HTML代碼:NG單擊不起作用

<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-點擊不適用於new_div?

+1

因爲你沒有編譯它!在setAttribute之後使用'$ compile'。 – Bettimms

+0

你介意寫下確切的命令嗎? –

回答

1

使用$compile應該工作

$compile(new_div)(scope) 
+0

如果我在函數內寫入'$ compile is not defined' –

+1

在指令中注入$ complie。 –

+1

[此鏈接](http://stackoverflow.com/questions/25759497/angularjs-dynamically-set-attribute)可能有幫助 –