简体   繁体   中英

Angular how to pass ngModel data from one component to another?

I am trying to pass ngModel data from one component to another, where the second component can use that data to execute a function in its html.

I am using @Input(), but I don't know how to send that data via hyperlink.

home.component.ts

import { Component, OnInit, Input } from '@angular/core';
import { BasePageComponent } from 'src/app/partials/base-page/base-page.component';
import { ActivatedRoute } from '@angular/router';
import { SurveyService } from 'src/app/services/survey.service';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent extends BasePageComponent implements OnInit {

  @Input() surveyName: string;
  surveys: any[];

  constructor(
    route: ActivatedRoute,
    private surveyService: SurveyService) {
    super(route);
   }

  ngOnInit() {
    this.surveys = this.surveyService.getAll();
  }
}

home.component.html

<div
    class="col-4 col-sm-4 col-md-4 col-lg-4"
    *ngFor="let survey of surveys"
>
<a class="btn btn-outline-secondary" href="/about" role="button">Begin Survey</a>
</div>

about.component.html

<input type="text" [(ngModel)]="surveyName" oninput="loadSurvey(surveyName)">

Right now, this doesn't do anything. My expected result is that, when I click on Begin Survey , the about.component.html will load the survey I clicked on with loadSurvey(surveyName) method in

You could use shared component.

In one component you can listen for changes and onChange you can transfer info to another component.

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Injectable()
export class DataService {

  private messageSource = new BehaviorSubject('default message');
  currentMessage = this.messageSource.asObservable();

  constructor() { }

  changeMessage(message: string) {
    this.messageSource.next(message);
  }

}

shared component could look simple like this.

when you want to send message you can use

this.data.changeMessage('your info');

and in second component where you recieve it.

this.data.currentMessage.subscribe(message => {
    console.log(message); //or whatever you want
}});

In both components you can import your service like: private data: DataService in constructor.

for more info you can watch and read this .

In your service file

import { BehaviorSubject } from 'rxjs';


surveyName : any;
private pathSource = new BehaviorSubject(window.location.pathname);
currentPath = this.pathSource.asObservable();

changeValue(message: string) {
    this.pathSource.next(message)
}

In your about component file

loadSurvey(surveyName){
    this.service.changeValue(surveyName);
}

In your home.component.ts file

ngOnInit() {
    this.service.changeValue.subscribe(message => {
        console.log(message); // you will get your survey name sended from home component
    })
}

Hope this will help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM