Controller called twice for custom directive inside ion-nav-buttons


#1

I build my own directive rs-notification to handle upcoming MQTT messages and provide visual feedback.

I put this directive inside the ion-nav-buttons component:

<ion-side-menu-content>
        <ion-nav-bar class="rs-header">
            <ion-nav-back-button>
            </ion-nav-back-button>
            <ion-nav-buttons side="left">
                <button class="button button-icon button-clear ion-navicon color-primary-light" menu-toggle="left"></button>
            </ion-nav-buttons>
            <ion-nav-buttons side="right">
                <rs-notification></rs-notification>
            </ion-nav-buttons>
        </ion-nav-bar>
        <ion-nav-view name="menuContent"></ion-nav-view>
    </ion-side-menu-content>

In that case the controller of rs-notification is called twice, if I put the directive in an higher level that won’t happen.
Am I doing something wrong?

(More details at http://stackoverflow.com/questions/33147537/angular-directives-controller-initialized-twice)


#2

Hello,

I looked at Your stackoverflow question - I don’t understand why You wrote the controller the way You did. Also: are You sure as far as the logic of Your application goes that You need to use a controller? You count badges in the controller, You might as well do that inside the directive.


#3

Hi psyche,

I’don’t understand what’s wrong in my controller, can You be more specific?


#4

Why did You write it as a variable and not

rsNotificationModule.controller() 

?

But honestly I don’t think this is the problem… I was just curious :smile:
Did You try using the

link

function in the directive?


#5

Thanks for the advice, I didn’t know the link function in directive, I will take a look at that.

Anyway I’d like to know if that problem it’s for something I got wrong or can be a ionic issue.


#6

Go to the angular docs: https://docs.angularjs.org/guide/directive and scroll to:
Creating a Directive that Adds Event Listeners

I hope that helps