0

In angularjs , I am making a treeview using directive.

return {
        restrict: "E",
        //replace: true,
        scope: {member: '=', ShowDetailsCtrlFunc : '&'},
        template: "<li><span ng-click=ShowDetailsCtrlFunc()>{{member.NodeName}}</span></li>",       
        controller: 'MainCtrl',
        link: linkerfunc        
    }

Things are fine, but i observed that controller function calls multiple times while loading. Would be a great learning if explain me the concept behind this. Or is it possible to handle this and let the function execute only once ? I have created below plunker for same.

plunkr

Thanks

usersam
  • 1,125
  • 4
  • 27
  • 54
  • you have added controller: 'MainCtrl', which triggers the controller, since you need to use $scope.ShowDetailsCtrlFunc function on controller you need to use controller: 'MainCtrl'. Maybe you can use a rootScope check for the $timeout(function(){ $scope.Intialfunc(); }, 1000); not execute only once – twitch Feb 07 '18 at 17:45
  • https://stackoverflow.com/a/15681173/4222181 – Stanislav Kvitash Feb 07 '18 at 17:57
  • @twitch, the rootScope seems an option. I tried to implement but not now that click function ShowDetailsCtrlFunc stopped working and shows undefined. the updated plunkr is , http://plnkr.co/edit/EQy9F0C9Uu37JFKVTAn3?p=preview – usersam Feb 08 '18 at 04:38
  • I am not if this will be of any help https://plnkr.co/edit/PMYsVZfaujHVK1MoVL6N?p=preview – twitch Feb 08 '18 at 05:15

0 Answers0