2

I'm supposed to use this third party framework and have to initialize it after Angular has initialized.

I've tried adding it to my index.html:

<body>
  ...
<script>
    document.addEventListener('DOMContentLoaded', () => {
        console.log('DKFDS Initialized', DKFDS);
        DKFDS.init();
    });
</script>
</body>

but that doesn't work.

So I tried adding it to ngOnInit() in app.component.ts:

ngOnInit() {
    document.addEventListener('DOMContentLoaded', () => {
        console.log('DKFDS Initialized', DKFDS);
        DKFDS.init();
    });
}

still no luck :-(

The only way I can get it to work is by using jQuery to call the init() function:

ngOnInit() {
  $(() => {
    console.log('DKFDS Initialized', DKFDS);
      DKFDS.init();
    });
}

EDIT: I've also tried ngAfterViewInit() with the same disappointing result...

So my question is: How do I make this work in a pure Angular app without jQuery?

I've made a stackblitz to try to illustrate the issue. A menu should popup by clicking the "Overflow menu": https://stackblitz.com/edit/angular-ubsvkt

3
  • 2
    have you tried ngAfterViewInit ? Commented Oct 30, 2019 at 11:10
  • Call your function in ngAfterViewInit becasue DOMContentLoaded equivalent to ngAfterViewInit Commented Oct 30, 2019 at 11:12
  • Updated OP: ngAfterViewInit() didn't work either. Commented Oct 30, 2019 at 11:29

4 Answers 4

1

In such a case, you can initialize the third party lib inside ngAfterViewChecked

ngAfterViewChecked() { 
      DKFDS.init();
}

For angular Lifecycle Hooks refer: https://angular.io/guide/lifecycle-hooks

Sign up to request clarification or add additional context in comments.

2 Comments

I tried that but then DKFDS.init() was called all the time... :-)
@CJe , You should use a flag variable to check if it is initialized once.
0

See the updated stackblitz example, now it works,

You can find the dkfds dependency installed, and now the dropdown menu works as expexted. The problem was Angular doesn't know what that library was. Here is the dkfds npm page, you can install into your angular project with npm install --save dkfds

Comments

0

Add a setTimeOut to make it work like charm. Or use ngAfterViewInit() angular hook.

ngOnInit() {
  setTimeOut(() => {
   document.addEventListener('DOMContentLoaded', () => {
      console.log('DKFDS Initialized', DKFDS);
      DKFDS.init();
    });
  });   
}

Comments

0

You can show/hide the "overflow-menu-inner" div by toggling showOverflowMenu on button click.

Try like this:

Working Demo

<button (click)="showOverflowMenu = !showOverflowMenu"
  ...
</button>

    <div class="overflow-menu-inner" id="overflow1" *ngIf="showOverflowMenu">

        <ul class='overflow-list'>
            <li><button>Option 1</button></li>
            <li><button>Option 2</button></li>
            <li><a href='#'>Option 3 as link</a></li>
            <li><button>Option 4 is longer text</button></li>
        </ul>

    </div>

3 Comments

This works nicely :-) The issue is that the overflow menu is not the only component of the DKFDS framework. So I'll have to do workarounds for the others too :-/
This is the way things work in Angular, why do want to do it in jquery way?
Oh I don't want to do it using jQuery - definitely not. That was my entire issue - how to get it working w/o jQuery :-)

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.