Modifying the component's property value with an observable in Angular 2

In my project, I have implemented two components that share a service. The first component contains 2 buttons which trigger the methods squeezeContent() and unsqueezeContent() on click. These methods pass a numeric value to an observable in the shared service. This value is then subtracted from a property in another component that also shares the same service. I have been struggling to correctly utilize observables in this scenario.

First Component

import { Component, AfterViewInit, ElementRef, ViewChild } from '@angular/core';
import { CrosspropertiesService } from "../services/crossproperties.service";
@Component({
  selector: 'app-timer',
  templateUrl: './timer.component.html',
  styleUrls: ['./timer.component.css']
})
export class TimerComponent implements AfterViewInit {
    @ViewChild('timerBody') timerBody:ElementRef;
    constructor(private crossproperties: CrosspropertiesService ) { }
        public timerBodyHeight:number;
        ngAfterViewInit() {
            this.timerBodyHeight = this.timerBody.nativeElement.offsetHeight;
         }
         squeezeContent(){
            this.crossproperties.resizeHeight(this.timerBodyHeight);
         }
         unsqueezeContent(){
             this.crossproperties.resizeHeight(0);
         }
}

Service File

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class CrosspropertiesService {
  private subject  = new Subject<any>();

  resizeHeight(height:number){
    this.subject.next({ value: height });
  }
  getSidebarScrollHeight(): Observable<any>{
    return this.subject.asObservable();
  }
  constructor() { }
}

Second Component

import { Component, OnInit , OnDestroy, ElementRef, ViewChild} from '@angular/core';
import { CrosspropertiesService } from '../services/crossproperties.service';
import { Subscription } from 'rxjs/Subscription';
@Component({
  selector: 'app-sidebar-expanded',
  templateUrl: './sidebar-expanded.component.html',
  styleUrls: ['./sidebar-expanded.component.css']
})
export class SidebarExpandedComponent implements OnInit {
  subscription:Subscription;
  private sidebarscrollheight:number;
  constructor(private crossproperty: CrosspropertiesService) {
    this.subscription = this.crossproperty.getSidebarScrollHeight().subscribe(height => { this.sidebarscrollheight = this.sidebarscrollheight - height; });
  }
  ngOnInit() {  
   this.sidebarscrollheight = 600; //computed value in this section
  }  
}

Now, I need assistance with updating the value of the sidebarscrollheight property when the squeezeContent() and unsqueezeContent() methods in component 1 call the function in the shared service. The sidebarscrollheight property already holds a computed value. Any help would be greatly appreciated.

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

Having trouble generating a unique BlockEmbed with Quill 2.0-dev3

After using Quill 1.3.7 successfully with custom embed blots, we decided to upgrade to 2.0.0-dev3 for new features. However, this upgrade caused our custom blots to break, resulting in the error message: Class constructor BlockEmbed cannot be invoked with ...

Angular-template static functions refer to functions that do not require an

Our project utilizes the linting-config provided by AirBnB. There is a rule that stipulates class methods must utilize this or be declared as static. While this rule theoretically makes sense, it seems to present challenges within an angular context. Consi ...

The .ts document is not recognized as a TypeScript module

Check out this SystemJS + TypeScript plunk, which was created using the official Angular plunk template. An error is being thrown: (SystemJS) SyntaxError: Missing initializer in const declaration at eval () ... This error occurs when the .ts file is t ...

When altering the value format, the input shows NaN

Inside a mat form field, I have an input: <mat-form-field style="padding-right: 10px;"> <input matInput type="text" [ngModel]="value | formatSize" (ngModelChange)="value=+$event"; placeholder="value" [ngModelOptions]="{standalone: true}"&g ...

Charts are not displaying properly in Angular 10 when using ng2-charts

My application is supposed to display charts, but for some reason, the charts are not loading. When I check the DOM, I see the element being created with ==$0, which is confusing to me. I am using Angular Material, but I don't think that should be a ...

When utilizing *NgIf, the button will be shown without the accompanying text being displayed

When trying to display either a confirm or cancel button based on a boolean set in my component.ts, I implemented the following code in my HTML: <mat-dialog-actions class="dialog-actions"> <button class="cancel-btn" ...

What is the best way to implement individual error handling for each function within combineLatest?

I'm looking for guidance on how to handle errors in each function within this `combineLatest` operation. Check out the code snippet below: const combined = combineLatest( this.myservice1.fn1(param), this.myservice2.fn2(), this.myservice3.fn3() ...

The Angular Button fails to display when all input conditions are met

I'm currently working on validating a form using this link. The requirement is simple - when an input field is invalid, the `save` button should be disabled. Conversely, when all input fields are valid, the `SAVE` button should be enabled/shown. The & ...

In what scenarios would one utilize the share feature in the Angular HttpClient?

Is it the best approach to include share() in a request in Angular? public find(params: any): Observable<any> { return this.httpClient.get( `${environment.apiUrl}/${this.PATH}`, { params: HttpParamsBuilder.buildQueryParams(pa ...

Issue with mapping HttpClient subscribe method in Angular 5

Retrieve some data from an API and store it in a JSON array: [{"id":19,"date":{"date":"2017-09-10 10:58:40.000000","timezone_type":3,"timezone":"Europe/Paris"},"user":{"nom":"castro","prenom":"diana","mail":"<a href="/cdn-cgi/l/email-protection" class= ...

Upgrading Angular causes issues with fileReplacements not functioning properly for assets

Since upgrading Angular, I have encountered an issue where fileReplacements no longer work with assets. Here is the code snippet that I am using: "fileReplacements": [ { "replace": "src/assets/scss/x.scss", ...

Can components be SSGed individually rather than entire pages?

I am currently working with Next.js and I am wondering if there is a way to statically generate and display the database values in the header and footer components used across all pages. While getStaticProps can generate pages statically, it doesn't ...

How can I properly send a file to express-fileupload using a post request in Angular 9?

I have successfully accomplished this task in Postman by following this link: https://i.sstatic.net/dKs8b.png However, when attempting to do the same in Angular, the functionality does not seem to be working. Below is the Angular code snippet: <input ...

Optimizing Angular 2+ for Search Engine Crawlers

My angular 4+ web application has a unique header for each route, with all components loading through Angular code which mainly consists of JavaScript. This setup is causing Google to be unable to crawl the links effectively, impacting SEO. If I were to ad ...

What is the specific reference of the first parameter in NLS localize?

Regarding the question on localizing VSCode extensions, I am also curious why the localize function requires two parameters. When it comes to localizing settings, it's a simple process of replacing literal values with tokens surrounded by percent sig ...

Challenges arise when configuring routing in angular, nginx, and docker and encountering issues upon

Currently, I have set up angular with nginx as a reverse proxy within a docker container. The issue I am facing is related to the routing of the angular application. While I can smoothly navigate through various pages using the navbar, reloading the page ...

Utilizing the axios create method: troubleshooting and best practices

I am attempting to use the axios library in my Next.js app (written in TypeScript) to access a public API for retrieving IP addresses from . In my index.ts file, I have the following code: import axios from "axios"; export const ipApi = axios.cr ...

How to eliminate certain elements from the DOM in Angular 5

I'm facing a minor issue in my Angular/Typescript project that I can't seem to resolve. I am still new to these technologies and struggling with removing certain DOM elements. The content is auto-generated with specific CSS classes, and unfortuna ...

The argument labeled as 'State' cannot be assigned to a parameter labeled as 'never'

I've recently delved into using TypeScript with React. I attempted to incorporate it with the React useReducer hook, but I hit a roadblock due to an unusual error. Below is my code snippet: export interface ContractObj { company: string; ...

You haven't included an index signature in the TypeScript type

I am creating MyInterface.dic to function as a dictionary with the format name: value, and here is how it is defined: interface MyInterface { dic: { [name: string]: number } } Next, I am writing a function that expects my specific type: function foo(a ...