Bonjour,

En essayant d'utiliser le menu dynamique proposé par https://stackblitz.com/edit/dynamic-...%2Fnav-item.ts dans mon projet angular (version 9.0.5) qui utilise le template Material Dashboard Angular (https://www.creative-tim.com/product...board-angular2), je rencontre une anomalie au niveau de l'affichage de menu. En chargeant ma page web, seul le premier élément de menu s'affiche, les autres éléments de menu s'affichent l'un après l'autre en cliquant sur le premier élément affiché.

L'intégration de l'exemple de menu dans mon projet est comme suit: j'ai placé le composant MenuItemComponent sous le dossier "components" et je l'ai importé et déclaré dans le module "ComponentsModule" puis j'ai placé le code de menu dans le template "nav-bar.component.html" après avoir déclaré le tableau contenant les éléments de menu dans le composant "NavbarComponent" comme suit:

Code Typescript : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 // ComponentsModule 
 
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
 
import { FooterComponent } from './footer/footer.component';
import { NavbarComponent } from './navbar/navbar.component';
import { SidebarComponent } from './sidebar/sidebar.component';
import { MenuItemComponent } from './menu-item/menu-item.component';
 
 
 
import { MatButtonModule} from '@angular/material/button';
import { MatIconModule} from '@angular/material/icon';
import { MatMenuModule} from '@angular/material/menu';
import { MatToolbarModule} from '@angular/material/toolbar';
 
 
@NgModule({
  exports: [
    FooterComponent,
    NavbarComponent,
    SidebarComponent,
    MenuItemComponent,
  ],
  imports: [
    CommonModule,
    RouterModule,
 
    MatIconModule,
    MatButtonModule,
    MatMenuModule,
    MatToolbarModule
 
  ],
  declarations: [
    FooterComponent,
    NavbarComponent,
    SidebarComponent,
    MenuItemComponent,
  ],
 
})
export class ComponentsModule { }

Code Typescript : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
  // NavbarComponent
 
import { Component, OnInit, ElementRef } from '@angular/core';
import { ROUTES } from '../sidebar/sidebar.component';
import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
import { Router } from '@angular/router';
import {NavItem} from '../nav-item';
 
@Component({
  selector: 'app-navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.css']
})
export class NavbarComponent implements OnInit {
    private listTitles: any[];
    location: Location;
      mobile_menu_visible: any = 0;
    private toggleButton: any;
    private sidebarVisible: boolean;
 
    constructor(location: Location,  private element: ElementRef, private router: Router) {
      this.location = location;
          this.sidebarVisible = false;
    }
 
    ngOnInit(){
      this.listTitles = ROUTES.filter(listTitle => listTitle);
      const navbar: HTMLElement = this.element.nativeElement;
      this.toggleButton = navbar.getElementsByClassName('navbar-toggler')[0];
      this.router.events.subscribe((event) => {
        this.sidebarClose();
         var $layer: any = document.getElementsByClassName('close-layer')[0];
         if ($layer) {
           $layer.remove();
           this.mobile_menu_visible = 0;
         }
     });
    }
 
    sidebarOpen() {
        const toggleButton = this.toggleButton;
        const body = document.getElementsByTagName('body')[0];
        setTimeout(function(){
            toggleButton.classList.add('toggled');
        }, 500);
 
        body.classList.add('nav-open');
 
        this.sidebarVisible = true;
    };
    sidebarClose() {
        const body = document.getElementsByTagName('body')[0];
        this.toggleButton.classList.remove('toggled');
        this.sidebarVisible = false;
        body.classList.remove('nav-open');
    };
    sidebarToggle() {
        // const toggleButton = this.toggleButton;
        // const body = document.getElementsByTagName('body')[0];
        var $toggle = document.getElementsByClassName('navbar-toggler')[0];
 
        if (this.sidebarVisible === false) {
            this.sidebarOpen();
        } else {
            this.sidebarClose();
        }
        const body = document.getElementsByTagName('body')[0];
 
        if (this.mobile_menu_visible == 1) {
            // $('html').removeClass('nav-open');
            body.classList.remove('nav-open');
            if ($layer) {
                $layer.remove();
            }
            setTimeout(function() {
                $toggle.classList.remove('toggled');
            }, 400);
 
            this.mobile_menu_visible = 0;
        } else {
            setTimeout(function() {
                $toggle.classList.add('toggled');
            }, 430);
 
            var $layer = document.createElement('div');
            $layer.setAttribute('class', 'close-layer');
 
 
            if (body.querySelectorAll('.main-panel')) {
                document.getElementsByClassName('main-panel')[0].appendChild($layer);
            }else if (body.classList.contains('off-canvas-sidebar')) {
                document.getElementsByClassName('wrapper-full-page')[0].appendChild($layer);
            }
 
            setTimeout(function() {
                $layer.classList.add('visible');
            }, 100);
 
            $layer.onclick = function() { //asign a function
              body.classList.remove('nav-open');
              this.mobile_menu_visible = 0;
              $layer.classList.remove('visible');
              setTimeout(function() {
                  $layer.remove();
                  $toggle.classList.remove('toggled');
              }, 400);
            }.bind(this);
 
            body.classList.add('nav-open');
            this.mobile_menu_visible = 1;
 
        }
    };
 
    getTitle(){
      var titlee = this.location.prepareExternalUrl(this.location.path());
      if(titlee.charAt(0) === '#'){
          titlee = titlee.slice( 1 );
      }
 
      for(var item = 0; item < this.listTitles.length; item++){
          if(this.listTitles[item].path === titlee){
              return this.listTitles[item].title;
          }
      }
      return 'Dashboard';
    }
 
 
 
    navItems: NavItem[] = [
      {
        displayName: 'DevFestFL',
        iconName: 'close',
        children: [
          {
            displayName: 'Speakers',
            iconName: 'group',
            children: [
              {
                displayName: 'Michael Prentice',
                iconName: 'person',
                route: 'michael-prentice',
                children: [
                  {
                    displayName: 'Create Enterprise UIs',
                    iconName: 'star_rate',
                    route: 'material-design'
                  }
                ]
              },
              {
                displayName: 'Stephen Fluin',
                iconName: 'person',
                route: 'stephen-fluin',
                children: [
                  {
                    displayName: 'What\'s up with the Web?',
                    iconName: 'star_rate',
                    route: 'what-up-web'
                  }
                ]
              },
              {
                displayName: 'Mike Brocchi',
                iconName: 'person',
                route: 'mike-brocchi',
                children: [
                  {
                    displayName: 'My ally, the CLI',
                    iconName: 'star_rate',
                    route: 'my-ally-cli'
                  },
                  {
                    displayName: 'Become an Angular Tailor',
                    iconName: 'star_rate',
                    route: 'become-angular-tailer'
                  }
                ]
              }
            ]
          },
          {
            displayName: 'Sessions',
            iconName: 'speaker_notes',
            children: [
              {
                displayName: 'Create Enterprise UIs',
                iconName: 'star_rate',
                route: 'material-design'
              },
              {
                displayName: 'What\'s up with the Web?',
                iconName: 'star_rate',
                route: 'what-up-web'
              },
              {
                displayName: 'My ally, the CLI',
                iconName: 'star_rate',
                route: 'my-ally-cli'
              },
              {
                displayName: 'Become an Angular Tailor',
                iconName: 'star_rate',
                route: 'become-angular-tailer'
              }
            ]
          },
          {
            displayName: 'Feedback',
            iconName: 'feedback',
            route: 'feedback'
          }
        ]
      },
      {
        displayName: 'Disney',
        iconName: 'close',
        children: [
          {
            displayName: 'Speakers',
            iconName: 'group',
            children: [
              {
                displayName: 'Michael Prentice',
                iconName: 'person',
                route: 'michael-prentice',
                children: [
                  {
                    displayName: 'Create Enterprise UIs',
                    iconName: 'star_rate',
                    route: 'material-design'
                  }
                ]
              },
              {
                displayName: 'Stephen Fluin',
                iconName: 'person',
                route: 'stephen-fluin',
                children: [
                  {
                    displayName: 'What\'s up with the Web?',
                    iconName: 'star_rate',
                    route: 'what-up-web'
                  }
                ]
              },
              {
                displayName: 'Mike Brocchi',
                iconName: 'person',
                route: 'mike-brocchi',
                children: [
                  {
                    displayName: 'My ally, the CLI',
                    iconName: 'star_rate',
                    route: 'my-ally-cli'
                  },
                  {
                    displayName: 'Become an Angular Tailor',
                    iconName: 'star_rate',
                    route: 'become-angular-tailer'
                  }
                ]
              }
            ]
          },
          {
            displayName: 'Sessions',
            iconName: 'speaker_notes',
            children: [
              {
                displayName: 'Create Enterprise UIs',
                iconName: 'star_rate',
                route: 'material-design'
              },
              {
                displayName: 'What\'s up with the Web?',
                iconName: 'star_rate',
                route: 'what-up-web'
              },
              {
                displayName: 'My ally, the CLI',
                iconName: 'star_rate',
                route: 'my-ally-cli'
              },
              {
                displayName: 'Become an Angular Tailor',
                iconName: 'star_rate',
                route: 'become-angular-tailer'
              }
            ]
          },
          {
            displayName: 'Feedback',
            iconName: 'feedback',
            route: 'feedback'
          }
        ]
      },
      {
        displayName: 'Orlando',
        iconName: 'close',
        children: [
          {
            displayName: 'Speakers',
            iconName: 'group',
            children: [
              {
                displayName: 'Michael Prentice',
                iconName: 'person',
                route: 'michael-prentice',
                children: [
                  {
                    displayName: 'Create Enterprise UIs',
                    iconName: 'star_rate',
                    route: 'material-design'
                  }
                ]
              },
              {
                displayName: 'Stephen Fluin',
                iconName: 'person',
                route: 'stephen-fluin',
                children: [
                  {
                    displayName: 'What\'s up with the Web?',
                    iconName: 'star_rate',
                    route: 'what-up-web'
                  }
                ]
              },
              {
                displayName: 'Mike Brocchi',
                iconName: 'person',
                route: 'mike-brocchi',
                children: [
                  {
                    displayName: 'My ally, the CLI',
                    iconName: 'star_rate',
                    route: 'my-ally-cli'
                  },
                  {
                    displayName: 'Become an Angular Tailor',
                    iconName: 'star_rate',
                    route: 'become-angular-tailer'
                  }
                ]
              }
            ]
          },
          {
            displayName: 'Sessions',
            iconName: 'speaker_notes',
            children: [
              {
                displayName: 'Create Enterprise UIs',
                iconName: 'star_rate',
                route: 'material-design'
              },
              {
                displayName: 'What\'s up with the Web?',
                iconName: 'star_rate',
                route: 'what-up-web'
              },
              {
                displayName: 'My ally, the CLI',
                iconName: 'star_rate',
                route: 'my-ally-cli'
              },
              {
                displayName: 'Become an Angular Tailor',
                iconName: 'star_rate',
                route: 'become-angular-tailer'
              }
            ]
          },
          {
            displayName: 'Feedback',
            iconName: 'feedback',
            route: 'feedback'
          }
        ]
      },
      {
        displayName: 'Maleficent',
        disabled: true,
        iconName: 'close',
        children: [
          {
            displayName: 'Speakers',
            iconName: 'group',
            children: [
              {
                displayName: 'Michael Prentice',
                iconName: 'person',
                route: 'michael-prentice',
                children: [
                  {
                    displayName: 'Create Enterprise UIs',
                    iconName: 'star_rate',
                    route: 'material-design'
                  }
                ]
              },
              {
                displayName: 'Stephen Fluin',
                iconName: 'person',
                route: 'stephen-fluin',
                children: [
                  {
                    displayName: 'What\'s up with the Web?',
                    iconName: 'star_rate',
                    route: 'what-up-web'
                  }
                ]
              },
              {
                displayName: 'Mike Brocchi',
                iconName: 'person',
                route: 'mike-brocchi',
                children: [
                  {
                    displayName: 'My ally, the CLI',
                    iconName: 'star_rate',
                    route: 'my-ally-cli'
                  },
                  {
                    displayName: 'Become an Angular Tailor',
                    iconName: 'star_rate',
                    route: 'become-angular-tailer'
                  }
                ]
              }
            ]
          },
          {
            displayName: 'Sessions',
            iconName: 'speaker_notes',
            children: [
              {
                displayName: 'Create Enterprise UIs',
                iconName: 'star_rate',
                route: 'material-design'
              },
              {
                displayName: 'What\'s up with the Web?',
                iconName: 'star_rate',
                route: 'what-up-web'
              },
              {
                displayName: 'My ally, the CLI',
                iconName: 'star_rate',
                route: 'my-ally-cli'
              },
              {
                displayName: 'Become an Angular Tailor',
                iconName: 'star_rate',
                route: 'become-angular-tailer'
              }
            ]
          },
          {
            displayName: 'Feedback',
            iconName: 'feedback',
            route: 'feedback'
          }
        ]
      }
    ];
 
 
 
}
Code html : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  // template nav-bar.component.html
 
<nav class="navbar navbar-expand-lg navbar-transparent  navbar-absolute fixed-top">
	<div class="container-fluid">
        <div class="navbar-wrapper">
 
            <!----<a class="navbar-brand" href="javascript:void(0)">{{getTitle()}}</a> -->
         <span *ngFor="let item of navItems"> 
 
            <!-- Handle branch node buttons here -->
          <span *ngIf="item.children && item.children.length > 0">
              <button mat-raised-button color="primary" [matMenuTriggerFor]="menu.childMenu"  > 
                {{item.displayName}} 
                <mat-icon>expand_more</mat-icon>
              </button>
              <app-menu-item1 #menu [items]="item.children" ></app-menu-item1>
            </span>  
            <!-- Leaf node buttons here -->
           <span *ngIf="!item.children || item.children.length === 0">
              <button mat-button color="primary" [routerLink]="item.route">
                {{item.displayName}}
 
              </button>
            </span> 
            </span> 
 
 
 
 
 
        </div>
 
        <button mat-raised-button class="navbar-toggler" type="button" (click)="sidebarToggle()">
            <span class="sr-only">Toggle navigation</span>
            <span class="navbar-toggler-icon icon-bar"></span>
            <span class="navbar-toggler-icon icon-bar"></span>
            <span class="navbar-toggler-icon icon-bar"></span>
        </button>
        <div class="collapse navbar-collapse justify-content-end" id="navigation">
           <!-- <form class="navbar-form">
                <div class="input-group no-border">
                    <input type="text" value="" class="form-control" placeholder="Search...">
                    <button mat-raised-button type="submit" class="btn btn-white btn-round btn-just-icon">
                        <i class="material-icons">search</i>
                        <div class="ripple-container"></div>
                    </button>
                </div>
            </form> -->
 
 
 
 
 
            <ul class="navbar-nav">
               <!-- <li class="nav-item">
                    <a class="nav-link" href="javascript:void(0)">
                        <i class="material-icons">dashboard</i>
                        <p>
                            <span class="d-lg-none d-md-block">Stats</span>
                        </p>
                    </a>
                </li> -->
 
 
 
 
                <li class="nav-item dropdown">
                    <a class="nav-link" href="javascript:void(0)" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <i class="material-icons">notifications</i>
                        <span class="notification">5</span>
                        <p>
                            <span class="d-lg-none d-md-block">Some Actions</span>
                        </p>
                    </a>
                    <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownMenuLink">
                        <a class="dropdown-item" href="javascript:void(0)">Mike John responded to your email</a>
                        <a class="dropdown-item" href="javascript:void(0)">You have 5 new tasks</a>
                        <a class="dropdown-item" href="javascript:void(0)">You're now friend with Andrew</a>
                        <a class="dropdown-item" href="javascript:void(0)">Another Notification</a>
                        <a class="dropdown-item" href="javascript:void(0)">Another One</a>
                    </div>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="javascript:void(0)">
                        <i class="material-icons">power_settings_new</i>
                       <!-- <p>
                            <span class="d-lg-none d-md-block">Account</span>
                        </p> -->
                    </a>
                </li>
            </ul>
        </div>
    </div>
 
 
 
 
 
 
</nav>
 
 
 
<!--
 
<nav class="navbar navbar-transparent navbar-absolute">
    <div class="container-fluid">
        <div class="navbar-header">
            <button mat-raised-button type="button" class="navbar-toggle" data-toggle="collapse" (click)="sidebarToggle()">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="javascript:void(0)">{{getTitle()}}</a>
        </div>
        <div class="collapse navbar-collapse">
            <ul class="nav navbar-nav navbar-right">
                <li>
                    <a href="javascript:void(0)" class="dropdown-toggle" data-toggle="dropdown">
                        <i class="material-icons">dashboard</i>
                        <p class="hidden-lg hidden-md">Dashboard</p>
                    </a>
                </li>
                <li class="dropdown">
                    <a href="javascript:void(0)" class="dropdown-toggle" data-toggle="dropdown">
                        <i class="material-icons">notifications</i>
                        <span class="notification">5</span>
                        <p class="hidden-lg hidden-md">Notifications</p>
                    </a>
                    <ul class="dropdown-menu">
                        <li><a href="javascript:void(0)">Mike John responded to your email</a></li>
                        <li><a href="javascript:void(0)">You have 5 new tasks</a></li>
                        <li><a href="javascript:void(0)">You're now friend with Andrew</a></li>
                        <li><a href="javascript:void(0)">Another Notification</a></li>
                        <li><a href="javascript:void(0)">Another One</a></li>
                    </ul>
                </li>
                <li>
                    <a href="javascript:void(0)" class="dropdown-toggle" data-toggle="dropdown">
                       <i class="material-icons">person</i>
                       <p class="hidden-lg hidden-md">Profile</p>
                    </a>
                </li>
            </ul>
 
            <form class="navbar-form navbar-right" role="search">
                <div class="form-group form-black is-empty">
                    <input type="text" class="form-control" placeholder="Search">
                    <span class="material-input"></span>
                </div>
                <button mat-raised-button type="submit" class="btn btn-white btn-round btn-just-icon">
                    <i class="material-icons">search</i><div class="ripple-container"></div>
                </button>
            </form>
        </div>
    </div>
</nav> -->


Toute aide serait appréciée.
Merci d'avance.