angular / angularfire

Angular + Firebase = ❤️
https://firebaseopensource.com/projects/angular/angularfire2
MIT License
7.64k stars 2.2k forks source link

AngularFire not fetching conditional values on first load. #3060

Open michaeldever opened 2 years ago

michaeldever commented 2 years ago

Description

Version info

Angular:

^11.2.0

Firebase:

^8.9.1

AngularFire:

^6.1.5

Other (e.g. Ionic/Cordova, Node, browser, operating system):

Node: 17.0.1

How to reproduce these conditions

Steps to set up and reproduce

app.component.ts

export class AppComponent {
  constructor(
    private analytics: AngularFireAnalytics,
  ) {
      this.analytics.setUserProperties({
        user_attribute: user.attribute
      })
  }
 ...

element.directive.ts

import { Directive, ElementRef, Input, OnDestroy, OnInit } from '@angular/core';
import { AngularFireAnalytics } from '@angular/fire/analytics';
import { AngularFireRemoteConfig, Parameter } from '@angular/fire/remote-config';
import { Subscription } from 'rxjs';

/**
 * Directive to add feature flag to an element.
 */
@Directive({
  selector: '[elementRemoteFlag]',
})
export class ElementRemoteFlagDirective implements OnInit, OnDestroy {
  // NOTE: the remote flag to display this element for.
  @Input() elementRemoteFlag: string;

  private subscriptions: Subscription[] = [];
  private shouldShow = false;

  constructor(private element: ElementRef, private remoteConfig: AngularFireRemoteConfig, private analytics: AngularFireAnalytics) {
    this.element.nativeElement.style.display = 'none';
  }

  ngOnInit() {
    this.subscriptions.push(
      this.remoteConfig.changes.subscribe(
        (remoteParameter: Parameter) => {
          if (remoteParameter?.key === this.elementRemoteFlag && remoteParameter?._value === 'true') {
            this.shouldShow = true;
          }

          if (this.shouldShow) {
            this.element.nativeElement.style.display = '';
          } else {
            this.element.nativeElement.style.display = 'none';
          }
        },
        e => {
          console.log(e);
        }
      )
    );
  }

  ngOnDestroy() {
    this.subscriptions.forEach(subscription => subscription.unsubscribe());
  }
}

Sample data and security rules

N/A

Debug output

Errors in the JavaScript console

None.

Output from firebase.database().enableLogging(true);

N/A.

Screenshots

N/A

Expected behavior

That the remote config is loaded, based on the conditions for the user that I have set using setUserProperties

Actual behavior

Fynsky commented 9 months ago

We have the same problem with "@angular/common": "11.2.5" "@angular/fire": "6.1.5" "firebase": "8.9.1" @michaeldever are you still having the problem of getting conditional values, or has it been resolved?