1

Due to the requirements of project I need to send data value dataOne in app.component.ts to its app.module.ts so that I can store it in variable moduleValue. Please refer code below and suggest how can achieve this transfer of data within files.

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'formcheck';

  dataOne: any;

  getValues(val: any) {
    console.warn(val);
    this.dataOne = val;
  }
}

app.module.ts

import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

let moduleValue: any;

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}
  
}
6
  • Why do you want to store the value in your module and e.g., not in a service? Commented Jul 26, 2022 at 10:02
  • @Batajus after that I have to inject inside some library. I just want data to be stored inside moduleValue, please suggest however possible Commented Jul 26, 2022 at 10:08
  • if you don't care about proper separation of concerns that just do whatever is easiest - attach the value to window Commented Jul 26, 2022 at 10:16
  • @devmiles.com please could you share some example? Commented Jul 26, 2022 at 10:24
  • look at the answer from Mohamed Moumen below, his solution is even better because it's not introducing a global variable. Commented Jul 26, 2022 at 10:28

2 Answers 2

1

I'd suggest that you declare a variable or an observable in a service, however, since you mentioned that you need to do this within the same files you might want to export a variable from your component and import it into your module like so:

app.component.ts

export class AppComponent {
  title = 'formcheck';

  dataOne: any;

  getValues(val: any) {
    console.warn(val);
    this.dataOne = val;
    DATA = this.dataOne;
  }
}

export var DATA = '';

app.module.ts

import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { DATA } from './app.component';

let moduleValue: any;

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}
  
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer but export var DATA is not getting updated even if the value inside getValues() changes. Could you suggest how to update data dynamically.
0

Here is the solution using service, I used BehaviorSubject for dynamic updation of value.

https://stackblitz.com/edit/angular-9pitmh?file=src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.html,src%2Fapp%2Fapp.module.ts,src%2Fapp%2Fdata.service.ts,src%2Fapp%2Fhello.component.ts

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.