Harness the power of a global service with a customized pipe in Angular 2

Exploring the capabilities of Angular 2, I have developed a global service that houses an interface. Various components utilize this interface from the global service. When the interface is modified by one component, the changes are reflected in all child components.

Currently, I am attempting to manage this through a pipe. However, when the interface value is altered from a child component, the modifications do not propagate to other components.

The following code snippet represents my progress thus far:

import { Pipe, PipeTransform, EventEmitter } from '@angular/core';

import { GlobalService } from './global-service'
import { MyInterface } from './my-interface'

@Pipe({name: 'myPipe'})
export class MyPipe implements PipeTransform {

    private value: string;

    private _interface: MyInterface;
    private interfaceChanged: EventEmitter<MyInterface>;

    constructor(private globalService: GlobalService) {

        this._interface = globalService._interface;
        this.interfaceChanged = this.globalService
                                   .interfaceChanged
                                   .subscribe((newInterface: MyInterface) => {
                                        this._interface = newInterface;
                                   });
    }

    transform(value: string, args: any[]): string {
        for (var key in this.language) {
            if (key == value) {
                this.value = this._interface[key];
                break;
            }
        }
        return this.value;
    }
}

Additionally, a working sample can be found on Plunker

Answer №1

When using pure pipes, they will only run if the value or parameter has changed.

However, you have the option to set a pipe as impure, which means it will run every time change detection is triggered. It's important to note that this can significantly impact performance.

@Pipe({name: 'myPipe', pure: false})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Learn how to update scope variables in Angular.io's mat-autocomplete using the [displayWith] function feature

I'm encountering a problem where I am unable to update locally declared variables in the component controller that triggers the mat-autocomplete. The issue is that these variables are confined within a specific scope, preventing me from making any mod ...

Adding custom type definitions for an untyped npm module in TypeScript 2

Despite attempting various methods recommended in other sources, I am struggling to configure a TypeScript project that utilizes an untyped NPM module. Below is a simplified example along with the steps I have taken. Let's imagine, for this demonstra ...

Troubleshooting: In Typescript, encountering issues with the exclusion functionality

Having trouble excluding certain files from my TypeScript compilation due to temporary files created by my editor. My tsconfig.json file includes: "exclude": ["*flycheck*", "**/*flycheck*"] Currently using tsc --watch for monitoring changes and compiling ...

Utilizing OverlappingMarkerSpidifier in conjunction with sebm-angular2-google-map

I'm currently integrating OverlappingMarkerSpidifier using SebM Angular 2 Google Maps on angular2 2.0.0. After successfully loading the google maps API with the GoogleMapsAPIWrapper imported from the sebm module, I am running into an issue when execu ...

Vue 3 Electron subcomponents and routes not displayed

Working on a project in Vue3 (v3.2.25) and Electron (v16.0.7), I have integrated vue-router4 to handle navigation within the application. During development, everything works perfectly as expected. However, when the application is built for production, the ...

Exploring the use of Vue and Typescript - encountering the error message "Property ... is not found in type" twice

In my specific case, I believe the error I am encountering may have a different root cause than the common solutions found for it. Configuration-related issues could be at play. Here is the code snippet: export default { data() { return { asy ...

Searching for a unique Kendo Vertical Menu Angular with a custom design?

I am currently incorporating the Kendo UI component into my Angular project. While using the Vertical Menu, I am facing challenges in changing the style of its children elements. Can anyone suggest a way to modify the styles of both parents and children ...

What is the best way to eliminate the "#" symbol from a URL?

I'm currently updating a website that features a hash symbol in every link when switching pages. This causes the page to not refresh everytime I switch, instead it stays on the last position of the site. Where can I locate the code responsible for th ...

Error encountered while making an Angular POST request with object parameter

Need to make a post request to update the current user on the home page. The expected url for the post should be in this format: "http:......./user/id". However, when the request is sent, the url becomes "http:/...../user/[object%20Object]" which causes an ...

The condition will be false if a number is present, even if it is zero

I am facing an issue with a class containing an optional field called startDateHour: export class Test { startDateHour?: number; // more fields, constructor etc. } I need to perform an action only if the startDateHour exists: if (test.startDateHour ...

"Frustrating experience: Attempting to initiate a new project

After creating a new project using angular-cli (angular 5), I encountered a failure during the startup process (even though it was working fine last week): ERROR in C:/Users/Dev/CoursAngular/jsjobs/src/main.ts Module build failed: Error: C:\Users&bso ...

What is the best way to refine a return type to a more specific type in TypeScript?

While working with the mysql2/promise library in TypeScript, I encountered an issue. Here is a shortened snippet that illustrates the problem: import {FieldPacket, PoolConnection, RowDataPacket} from 'mysql2/promise'; let connection: PoolConnec ...

Exploring the world of data binding in Angular 4

I am facing an issue with a component that has a variable called 'id'. In this component, I call a service that takes the 'id' variable as a parameter. I assign the value of a radio button to the 'id' variable in my template. ...

After the recent update to rc2, the Angular2 quickstart app has suddenly stopped functioning as expected

After updating my angular2 app, originally based on the tutorial / quickstart app, from rc1 to rc2, everything seemed fine: npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="325f5b5c5b5f5346515a72001c021c0302" ...

Which is better: Utilizing ASP.NET Core or ASP.NET Core MVC in tandem with Angular for the

Currently in the planning stages of developing a website or web application using .NET Core for the backend and Angular for the frontend. One aspect that is proving to be confusing is whether to use ASP.NET Core or ASP.NET Core MVC on the backend. I'm ...

How to extract a template from a string in Angular and transfer current context variables

I am developing a basic component for table creation: @Component({ selector: 'admin-table', template: ` <table class='table table-bordered'> <thead> <th *ngFor='let column of columns'> ...

What is the process for creating a map in which the value type is determined by the key type?

Is it feasible to formulate a Map<K, V> in a manner where the type of the value relies on the type of the key, without explicitly specifying the key's type upon initializing the map? For instance: abstract class BaseA { a() {} } class ConcreteA1 ...

Is there a simple method I can use to transition my current react.js project to typescript?

I am currently working on a project using react.js and am considering converting it to typescript. I attempted following an article on how to make this conversion but have run into numerous bugs and issues. Is there a simpler method for doing this conver ...

Commitments, the Angular2 framework, and boundary

My Angular2 component is trying to obtain an ID from another service that returns a promise. To ensure that I receive the data before proceeding, I must await the Promise. Here's a snippet of what the component code looks like: export class AddTodoCo ...

What is the reason for not receiving a transpilation error when referencing a non-exported class?

As a relatively new learner of Typescript and Javascript (with a background in Java), I have been navigating the transition from a single large Typescript file containing multiple classes to utilizing multiple classes. While I have made progress with the i ...