usersam
usersam

Reputation: 1235

angularjs ng-click function is not working in directive

I am trying to bind a click function with an element template , defined in directive. Somehow it is not working for unknown reasons and not throwing any error either.

My html

<body>
    <div ng-app="mainApp">
        <div ng-controller="MyController">          
             <div id="Container" class="Container">
             <collection collection='testdata'></collection>
             </div>
        </div>              
    </div>
</body>

My JS

var mainApp = angular.module("mainApp", [])    
mainApp.directive('collection', function () {
    return {
        restrict: "E",
        replace: true,
        scope: {
            collection: '='
        },
        template: "<ul><member ng-repeat='member in collection' member='member'></member></ul>"
    }
})

mainApp.directive('member', function ($compile) {       
    //var NewImg = "<img ng-click=ShowChildren() src=../Images/Plus15.png></img>";  
    //var NewChild = "<li>"+NewImg+"<span>{{member.TagName}}</span></li>";
    //var NewChild = "<li><img id=dynamic src=../Images/Plus15.png></img><span>{{member.TagName}}</span></li>";
    var NewChild = "<li><img ng-click=ShowChildren(event) src=../Images/Plus15.png></img><span>{{member.TagName}}</span></li>";
    //var NewChild = "<li>{{member.TagName}}</li>";

    return {
        restrict: "E",
        replace: false,
        scope: {
            member: '='
        },
        template: NewChild,
        link: function (scope, element, attrs) {            
            var collectionSt = '<collection collection="member.children"></collection>';
            if (angular.isArray(scope.member.children)) {    

                element.append("<collection collection='member.children'></collection>"); 
                $compile(element.contents())(scope.$new())

                //$compile(collectionSt)(scope, function(cloned, scope)   {
                    //element.append(cloned); 
                  //});
            }
        }
    }
})

mainApp.controller('MyController', function ($scope) {
    $scope.data = [{"TagName":"MyRootNode","TagPath":">MyRootNode","children":[{"TagName":"LandXML","TagPath":">MyRootNode>ChildItems>LandXML","children":[{"TagName":"Units","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Units","children":[{"TagName":"Imperial","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[1]>Imperial","children":[]},{"TagName":"Project","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Project","children":[]},{"TagName":"Application","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Application","children":[{"TagName":"Author","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[2]>Author","children":[]},{"TagName":"Alignments","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Alignments","children":[]},{"TagName":"Roadways","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Roadways","children":[{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[1]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[2]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[3]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[4]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[5]","children":[]}]}]}]}]},{"TagName":"Surfaces","TagPath":">MyRootNode>ChildItems>Surfaces","children":[{"TagName":"Surface1","TagPath":">MyRootNode>ChildItems>ChildItems[2]>Surface1","children":[]},{"TagName":"Surface2","TagPath":">MyRootNode>ChildItems>ChildItems[2]>Surface2","children":[]}]}]}]     
    $scope.testdata = $scope.data[0].children       
});

function ShowChildren (event) { 
    console.log("in function ShowChildren");
    var src = ($(this).attr('src') === '../Images/Plus15.png')
         ? '../Images/Minus15.png'
         : '../Images/Plus15.png';
     $(this).attr('src', src);
    event.stopImmediatePropagation();   
}   

I noticed that , function ShowChildren executes when in put this in directive but then it again gives problem in "$(this).attr('src') ". And i want it to be in separate files because i will be moving all codes to different javascript file.

Please help me to resolve this.

Upvotes: 0

Views: 46

Answers (2)

Kaustubh Khare
Kaustubh Khare

Reputation: 3510

Your ShowChildren() function is out of the scope of the controller. Put that function into the controller.

controller

mainApp.controller('MyController', function ($scope) {
    $scope.data = [{"TagName":"MyRootNode","TagPath":">MyRootNode","children":[{"TagName":"LandXML","TagPath":">MyRootNode>ChildItems>LandXML","children":[{"TagName":"Units","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Units","children":[{"TagName":"Imperial","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[1]>Imperial","children":[]},{"TagName":"Project","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Project","children":[]},{"TagName":"Application","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Application","children":[{"TagName":"Author","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[2]>Author","children":[]},{"TagName":"Alignments","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Alignments","children":[]},{"TagName":"Roadways","TagPath":">MyRootNode>ChildItems>ChildItems[1]>Roadways","children":[{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[1]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[2]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[3]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[4]","children":[]},{"TagName":"Roadway","TagPath":">MyRootNode>ChildItems>ChildItems[1]>ChildItems[3]>Roadway[5]","children":[]}]}]}]}]},{"TagName":"Surfaces","TagPath":">MyRootNode>ChildItems>Surfaces","children":[{"TagName":"Surface1","TagPath":">MyRootNode>ChildItems>ChildItems[2]>Surface1","children":[]},{"TagName":"Surface2","TagPath":">MyRootNode>ChildItems>ChildItems[2]>Surface2","children":[]}]}]}]     
    $scope.testdata = $scope.data[0].children;     
    $scope.ShowChildren= function(event) { 
         console.log("in function ShowChildren");
         var src = ($(this).attr('src') === '../Images/Plus15.png')
          ? '../Images/Minus15.png'
          :  '../Images/Plus15.png';
          $(this).attr('src', src);
          event.stopImmediatePropagation();   
    }   
});

Directive

var NewChild = "<li><img ng-click=ShowChildren($event) src=../Images/Plus15.png></img><span>{{member.TagName}}</span></li>";

Upvotes: 1

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38683

use ng-click=ShowChildren($event) instead of ng-click=ShowChildren(event) and also your function should defined in the angular scope. like which is answered by @Kaustubh Khare

Upvotes: 1

Related Questions