Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

When I click on my HTML img, ng-click doesn't work. I don't understand why. This error happens in masonryPictureView.html

Main page - home.html

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

Template of directive ngPicture - masonryPictureView.html

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

Directive in 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");
            }
        }
    }
}])
share
    
You should use an anchor with an ngclick and wrap your image instead. – Wayne Ellery May 23 '15 at 1:11
    
this SO post might be of help: accessing-ng-click-in-custom-directive – Shehryar Abbasi May 23 '15 at 1:39
    
hey quick questions, are there any errors in the console? secondly, this extra comma }, at the end of the first directive's controller: isn't causing any issues right? thirdly, can you check to see if it reaches the second directive's controller: function by placing a console.log just above $scope.test, lastly, would you need to restrict: EA for the second directive? – Shehryar Abbasi May 23 '15 at 2:31
    
No i have any error. For extra comma }, it's me, i have forgot to delete it when i have pushed my code on stackOverflow. In my code i have a link below.When i place console.log above $scope.test it's work – Negrier Aurelien May 23 '15 at 12:27
up vote 0 down vote accepted

Where does "projectdescription.pictures" come from?, Is it an array? Could it be empty?. Look in the console for errors. I recommend you that you use your namespace instead of ng for your custom directives or attributes. I made a plunker demostrating that anytime an image is clicked the test method is fired.

It is possible that you missed the ng-transclude placeholder on the ngMasonry directive?

 template: '<div class="masonry"><ng-transclude></ng-transclude></div>',
share
    
projectdescription is my controllerAs. It's not empty, this part of the code work there isn't no problem. My problem is in my second directive, my controller is call but the scope is empty and don't contain the function scope.test – Negrier Aurelien May 23 '15 at 12:38
    
Have a look at the plunker that I created. the test function is triggered when clicking in the image – Raulucco May 23 '15 at 14:06
    
on your plunker the link of your image is not valid. And even if i change the url of the src image and that i click on the image nothing is happening plnkr.co/edit/EGcWke3v33sVFORUrb4o?p=preview – Negrier Aurelien May 23 '15 at 14:22
    
I thought you wanted to trigger the test function. As you said that ngclick doesn't work – Raulucco May 23 '15 at 15:13
    
Yes i want to trigger the test function when i click on the image – Negrier Aurelien May 23 '15 at 15:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.