IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Angular Discussion :

ViewChild / ViewChildren


Sujet :

Angular

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre habitué
    Homme Profil pro
    Développeur Web
    Inscrit en
    Septembre 2020
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Septembre 2020
    Messages : 12
    Par défaut ViewChild / ViewChildren
    Salut tout le monde

    Je galère vraiment à comprendre le fonctionnement de ViewChild / ViewChildren
    Je cherche à récupérer les ElementRef d'un composant généré en *ngFor via un appel à une BDD firebase

    Est-ce que quelqu'un pourrait m'expliquer comment récupérer dans ParentComponent.ts tous les nativeElement des instances <app-child>?



    ParentComponent.html
    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    <main>
      <section #projectsWrapper *ngFor="let project of projects">
        <app-child #project [project]='project'> </app-child>
      </section>
    </main>

    Code : 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
    ParentComponent.tsimport { Component, ElementRef, OnInit, QueryList, ViewChild, ViewChildren } from '@angular/core';
    import { Subscription } from 'rxjs';
    import { DatabaseService } from './../database.service';
    import { Project } from './../models/project.model';
    import { gsap, Power4 } from 'gsap';
    import { ChildComponent } from '../child/child.component';
     
    @Component({
      selector: 'app-parent',
      templateUrl: './parent.component.html',
      styleUrls: ['./parent.component.scss']
    })
    export class ParentComponent implements OnInit {
     
      childs = [];
     
      @ViewChild(ChildComponent)
      private child: ChildComponent;
     
      @ViewChildren(ChildComponent)
      public childsList: QueryList<ChildComponent>;
     
      sub: Subscription;  
      projects: Project[];
     
     
     
      constructor(
        private databaseService: DatabaseService
      ) { }
     
      ngOnInit(): void {     
     
      }
     
      ngAfterViewInit(){
     
        this.sub = this.databaseService.getChilds().subscribe(
          project => (
              this.onDataLoaded(project)
          )
        )
     
      }
     
      onDataLoaded(project: any) { 
     
     
     
     
     
      }
     
    }
    DatabaseService.ts
    Code : 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
    import { Injectable } from '@angular/core';
    import { AngularFirestore } from '@angular/fire/firestore';
    import { AngularFireStorage} from '@angular/fire/storage';
    import { ChildComponent } from './child/child.component';
     
    @Injectable({
      providedIn: 'root'
    })
    export class DatabaseService {
     
      constructor(
        private db: AngularFirestore,
        private fstore:AngularFireStorage
      ) { }
     
     
     
     
      getChilds() {
        return this.db
                .collection<ChildComponent>('projects')
                .valueChanges();
      }
     
    }

  2. #2
    Membre très actif
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2019
    Messages
    707
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2019
    Messages : 707
    Par défaut
    essaye ça:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
     @ViewChildren("project")
      public childsList: QueryList<ChildComponent>;
     
      ngAfterViewInit(): void {
        console.log(this.childsList);
      }

  3. #3
    Membre habitué
    Homme Profil pro
    Développeur Web
    Inscrit en
    Septembre 2020
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Septembre 2020
    Messages : 12
    Par défaut
    Merci beaucoup @krakatoa
    C'était juste ça !!!

  4. #4
    Membre habitué
    Homme Profil pro
    Développeur Web
    Inscrit en
    Septembre 2020
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Septembre 2020
    Messages : 12
    Par défaut
    Bon en fait, ça marche si j'ajoute les app-child à la main mais pas avec l'appel firebase


    J'ai fait ça

    ParentComponent.ts
    Code : 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
    import { Component, ElementRef, OnInit, QueryList, ViewChild, ViewChildren } from '@angular/core';
    import { Subscription } from 'rxjs';
    import { DatabaseService } from './../database.service';
    import { Project } from './../models/project.model';
    import { gsap, Power4 } from 'gsap';
    import { ChildComponent } from '../child/child.component';
     
    @Component({
      selector: 'app-parent',
      templateUrl: './parent.component.html',
      styleUrls: ['./parent.component.scss']
    })
    export class ParentComponent implements OnInit {
     
      childs = [];
     
      @ViewChild(ChildComponent)
      private project: ChildComponent;
     
      @ViewChildren(ChildComponent)
      public childsList: QueryList<ChildComponent>;
     
      sub: Subscription;
      projects: Project[];
     
     
     
     
      constructor(
        private databaseService: DatabaseService
      ) { }
     
      ngOnInit(): void {
     
      }
     
      ngAfterViewInit(){
     
        this.sub = this.databaseService.getChilds().subscribe(
          projects => (
              this.onDataLoaded(projects)
          )
        )
     
      }
     
      onDataLoaded(projects: any) {
        this.projects = projects;
     
        console.log('projects', projects);
        console.log("childslist", this.childsList);
     
     
     
      }
     
    }

    ParentComponent.html

    Si j'ajoute à la main j'ai bien ma QueryList.length = 3

    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    <app-child> </app-child>
    <app-child> </app-child>
    <app-child> </app-child>


    Mais si je fais l'appel à la base, ma QueryList est vide..

    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    <app-child #childsList *ngFor="let project of projects" [project]='project'> </app-child>

  5. #5
    Membre très actif
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2019
    Messages
    707
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2019
    Messages : 707
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     @ViewChildren("childsList ")
      public childsList: QueryList<ChildComponent>;
     
      ngAfterViewInit(): void {
        console.log(this.childsList);
      }


    tu essayes de faire quoi, dans quel but ?

  6. #6
    Membre habitué
    Homme Profil pro
    Développeur Web
    Inscrit en
    Septembre 2020
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Septembre 2020
    Messages : 12
    Par défaut
    Merci de t'intéresser à mon problème

    J'essaie d'implémenter une animation GSAP et tout fonctionne bien à part quand je cherche à utiliser des composants générés depuis cette QueryList.
    Je sais pas si je suis clair.. J'ai fait un repo si ça peut t'aider à comprendre : https://github.com/plvbroker/public-lab.git (branche 'firebase')

    J'ai pu progresser grâce à tes conseils mais ça sort du thème principale de mon post parce que j'arrive maintenant à récupérer tous les ElementRef dans un tableau mais là encore l'animation ne fonctionne pas correctement
    Pourtant si tu regardes dans mon screenshot, dans la console le css est bien modifié par GSAP..

    Nom : q8eNg4mY3K.gif
Affichages : 402
Taille : 782,3 Ko

  7. #7
    Membre extrêmement actif
    Avatar de dukoid
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2012
    Messages
    2 100
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2012
    Messages : 2 100
    Par défaut
    tu veux dire que le css est modifié mais il n'est pas répercuté à l'écran ?


    chez moi, via firerfox et edge, je vois bien "project#01" disparaitre
    par contre sur chrome ça ne fonctionne pas

    c'est ça ton problème ?

+ Répondre à la discussion
Cette discussion est résolue.

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo