0

當我點擊我的HTML img時,ng-click不起作用。我不明白爲什麼。在發生這個錯誤masonryPictureView.htmlAngularJs ng-click不工作指令

主頁 - home.html的

<ng-masonry> 
    <ng-picture ng-items="projectdescription.pictures"></ng-picture> 
</ng-masonry> 

模板指令ngPicture的 - masonryPictureView.html

<ul class="grille"> 
<li ng-repeat="item in ngItems"> 
    <img ng-click="test()" src="{{item.img}}"/>  <------ NG-CLICK 
</li> 
</ul> 

指令在home.html的

app.directive('ngMasonry', [function() { 
    return { 
     restrict: 'E', 
     scope: {}, 
     transclude: true, 
     templateUrl: "shared/masonry/masonryView.html", 
     controller: function ($scope) { 
      xxxxxxxxx...... 
     }, 
    } 
}]) 

app.directive('ngPicture', ['$modal', '$log', function ($modal, $log) { 
    return { 
     require: "^ngMasonry", 
     restrict: 'E', 
     scope: { 
      ngItems: '=ngItems' 
     }, 
     templateUrl: "shared/masonry/masonryPictureView.html", 
     link: function (scope, element, attrs, ngMasonryCtrl) { 
      ngMasonryCtrl.setPictures(scope.ngItems); 
     }, 
     controller: function ($scope) { 
      $scope.test = function() < -- -- - function call in NG - CLICK { 
       console.log("toto"); 
      } 
     } 
    } 
}]) 
+0

您應該使用帶'ngclick'的定位點幷包裝圖像。 –

+0

這個SO帖子可能有幫助:[訪問ng中單擊自定義指令](http://stackoverflow.com/questions/24363217/angularjs-accessing-ng-click-in-custom-directive) –

+0

嘿快問題,在控制檯中是否有任何錯誤?其次,在第一個指令的控制器末尾加上這個額外的逗號'',是不是引起任何問題?第三,你可以通過在'$ scope.test'之上放置一個'console.log'來檢查它是否到達'second directive's controller:'函數,最後,你需要'restrict:EA' directive'? –

回答

0

哪裏 「projectdescription.pictures」 從何而來?它是一個數組?它可以是空的嗎?查看控制檯中的錯誤。我建議你使用你的名字空間而不是ng來存放你的自定義指令或屬性。我做了一個plunker演示,任何時候單擊圖像測試方法被激發。

您可能錯過了ngMasonry指令中的ng-transclude佔位符?

template: '<div class="masonry"><ng-transclude></ng-transclude></div>', 
+0

projectdescription是我的控制器。這不是空的,這部分代碼工作並不是沒有問題。我的問題是在我的第二個指令,我的控制器是調用,但範圍是空的,不包含函數scope.test –

+0

看看我創建的plunker。點擊圖像上的圖像 – Raulucco

+0

觸發測試功能,圖像鏈接無效。即使我改變了src圖像的URL,並且我點擊圖片上沒有任何事情發生http://plnkr.co/edit/EGcWke3v33sVFORUrb4o?p=preview –