Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
Are there any security risks using subjects in Angular this way?

Everytime I have to use a variable in different components which can be changed over time, I store that variable in a service and wrap the value in a Subject such that every component register changes when next is called. But then I see tutorials where it is mentioned that one has to be careful when using subjects. They do the following:

// in some service
private subject$: Subject<any> = new Subject();
subjectAsObservable: Observable<any> = this.subject$.asObservable();

and then the observable is used in the components but then I would not be able to call next to emit new values.

Is there any risk when using subject the following way:

// in some service
subject$: Subject<any> = new Subject();

and then subscribe to that subject in the components and if components make changes to the variable, next is called and every component that subscribes to that subject gets the new value.

Is the following implementation different (more "secure") to the implementation above:

private subject$: Subject<any> = new Subject();

emitNewValue(value: any): void {
    this.subject$.next(value);
}

getSubject(): Subject<any> {
    return this.subject$:
}

I do not quite get the security risks. How would I deal correctly with subjects?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It is about encapsulation. In larger applications, it is a good idea to have only one place that emits to the shared stream (subject), which is in the service.

Then no other component can, for example, accidentally call complete() on the subject. In the service, just provide one method in order to emit (emitNewValue in your example). A getter for the subject is not needed as exposing the subject with asObservable provides components with a read-Only copy of the subject, which they can listen/subscribe to.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda