Angular calling function double call twice

Below are two directives. My problem is that the addSectorAttributes function in my second directive was triggered twice by ng-click from project_subtypes.html.

      .directive('projectFilter', function($compile, $timeout){
            return {
                restrict: 'E',
                templateUrl: 'project_filter.html',
                link : function(scope,element, attr){
                   //getting data and fill the html from a service 
                },
                controller: function ($scope) {
                    $scope.addSubTypes = function (event,id) {
                      var ex = $(event.currentTarget);

                      var el = $compile( "<project-subtypes typeid="+id+" class='slide-content'></project-subtypes>" )( $scope );
                        ex.parent().append(el);

                    };
                }
            }
        })

       .directive('projectSubtypes', function($compile){
                return {
                    restrict: 'E',
                    scope: true,
                    templateUrl: 'project_subtypes.html',
                    link : function(scope,element,attr){
                            //getting data and fill the html from a service
                           var id = attr.typeid; 
sectorFiltering.getSectorSubTypes(id).success(function(data) {
                        scope.sector_subtypes = data.sector_subtypes;
                    });
                        });
                    },
                    controller: function ($scope) {
                        $scope.addSectorAttributes = function (event,id) {
                            console.log("teeest"); // called twice.....
                        };
                    }                        
                }
            })

      

project_subtypes.html

<li ng-repeat="subtype in substype.subtypes">
        <div layout='row' class='load-attributes' ng-click=" addSectorAttributes($event, subtype.id)"></div>
 </li>

      

Any help please ....

+3


source to share


1 answer


If a directive function is called twice, if once it doesn't give you subtype.id and next time if it gives you subtype.id, you just need to add a condition to your directive as shown below.

subtype.id && addSectorAttributes ($ event, subtype.id) "



Regards, Mahenrdra

0


source







All Articles