Как передать значение из шаблона HTML в компонент для последующего использования в сервисе - PullRequest
0 голосов
/ 26 октября 2018

Я хочу получить идентификатор из component.html в component.ts, чтобы передать его службе.

.ts файл:

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http'
import { HttpErrorResponse } from '@angular/common/http/src/response';
import { SendUsingApiService } from '../send-using-api.service';
import { Router, ActivatedRoute } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { setDefaultService } from 'selenium-webdriver/chrome';

@Component({
  selector: 'app-org-info',
  templateUrl: './org-info.component.html',
  styleUrls: ['./org-info.component.css'],
  providers: [SendUsingApiService]
})

export class OrgInfoComponent implements OnInit {
  orgData: string[] = [];
  Id = 1;

  editRecord:FormGroup;
  constructor(private httpService: HttpClient, private _serv: SendUsingApiService, 
    private fb: FormBuilder, private _ar:ActivatedRoute, private _r:Router) {
      this.editRecord = this.fb.group({
        Id:['1', []],
        OrganisationName:['', []],
        ContactPerson:['', []],
        ContactPersonHPNo:['', []],
        ContactPersonEmailId:['', []]

      });
    }

  ngOnInit() {
    console.log(this._ar.snapshot.params.Id, "+ve");
    this._ar.params.subscribe(() => {
      this._serv.getUsers(this._ar.snapshot.params.Id).subscribe((res)=>{
        console.log(res);
        this.setUser(res);
      });
  });

}

Я получаю значение для консоли.log (this._ar.snapshot.params.Id);как неопределенное "+ ve".

Я хочу получить значение Id в консоли.

Согласно запросам, я добавляю html-часть, хотя и немного скорректированную;

<td style="text-align: center;">
            <a class="btn btn-basic" [routerLink]="['/org-info',data['Id']]" role="button" (click)="getOrgData(data.Id)">View</a>&nbsp;&nbsp;&nbsp;&nbsp;
          </td>

1 Ответ

0 голосов
/ 29 октября 2018

Я определил свойство вместо Id = 1;(выше)

paramId = '';

затем, в пределах ngOnInit;

ngOnInit() {
this.paramId = this._ar.snapshot.params.Id;
console.log(paramId, "+ve");                                                         
}

При этом я получил значение Id вместо undefined.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...