0

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");
            }
        }
    }
}])
4
  • You should use an anchor with an ngclick and wrap your image instead. Commented May 23, 2015 at 1:11
  • this SO post might be of help: accessing-ng-click-in-custom-directive Commented May 23, 2015 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? Commented May 23, 2015 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 Commented May 23, 2015 at 12:27

1 Answer 1

0

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>',
Sign up to request clarification or add additional context in comments.

7 Comments

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
Have a look at the plunker that I created. the test function is triggered when clicking in the image
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
I thought you wanted to trigger the test function. As you said that ngclick doesn't work
Yes i want to trigger the test function when i click on the image
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.