Я новичок в Angular.Пока что мне понравилось, но я столкнулся с некоторыми трудностями в этом следующем случае.Я хочу отправить данные из одного компонента в другой компонент, как только пользователь щелкнет по определенной ячейке с тегом href в Datatables.Поэтому я создал следующий файл .ts
service.ts
import { Injectable } from '@angular/core';
import { HttpClient,HttpErrorResponse } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { BehaviorSubject } from 'rxjs';
@Injectable()
export class MyService {
private protDataSource = new BehaviorSubject<any>({
currentprotData:null
});
currentprotData= this.protDataSource.asObservable();
constructor(
private http: HttpClient
) {}
sendProtData(currentprotData:any){
console.log(currentprotData)
this.protDataSource.next(currentprotData);
}
get(url){
return this.http.get(url)
.pipe(
map(responce=>responce),
catchError(this.handleError)
)
}
}
sender.ts
import { Component, OnInit, OnDestroy, ViewChild, Renderer } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { DataTableDirective } from 'angular-datatables';
import { HttpClient,HttpErrorResponse } from '@angular/common/http';
import { Subject } from 'rxjs';
import { MyService } from '../qmpkb/qmpkb.service';
declare var jquery: any;
@Component({
selector: 'app-sender-page',
templateUrl: './sender.component.html',
styleUrls: ['./sender.component.css'],
providers:[MyService]
})
export class SenderComponent implements OnInit,OnDestroy {
dtOptions: DataTables.Settings = {};
dtTrigger: Subject<any> = new Subject();
private routeSub:any;
errorStr:Boolean;
@ViewChild(DataTableDirective)
datatableElement: DataTableDirective;
constructor(
private route: ActivatedRoute,
private http: HttpClient,
private renderer: Renderer,
private router: Router,
private _myserve:MyService
) { }
ngOnInit(): void {
//let destPath='fileapi/resultFile/jsonData/resultJson/'+this.queryResPath
let destPath='assets/json/fake.json'
this.dtOptions = {
pagingType: 'full_numbers',
ajax: destPath,
processing: true,
serverSide: false,
columns: [
{
title: 'Prot Seq',
render: function (data: any, type: any, row: any, meta) {
if (row["Prot Seq"].trim().length > 0 ){
var array =(row["Prot Seq"].trim()).split('<br>');
array.forEach(function(element, index) {
array[index] ='<a target="_blank" href="/seqfeatures" routerLinkActive="active" seq-link-id=' + element+'>'+element+'</a>';
});
return array.join("<br>");
} else {
return 'No';
}
}
}
]
};
}
ngAfterViewInit(): void {
this.renderer.listenGlobal('document', 'click', (event) => {
if (event.target.hasAttribute("seq-link-id")) {
this._qmpkb.get(url).subscribe((response: any)=>{
this._myserve.sendProtData(response);
}, error=>{
this.errorStr = error;
})
}
})
}
ngOnDestroy(){
this.routeSub.unsubscribe();
this.dtTrigger.unsubscribe();
}
}
receive.ts
import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { HttpClient,HttpErrorResponse } from '@angular/common/http';
import { MyService } from '../qmpkb/qmpkb.service';
declare var require: any
@Component({
selector: 'app-prot-view',
templateUrl: './prot-view.component.html',
styleUrls: ['./prot-view.component.css'],
providers:[MyService]
})
export class ProtViewComponent implements OnInit,OnDestroy {
message:string
constructor(
private route: ActivatedRoute,
private http: HttpClient,
private router: Router,
private _myserve:MyService,
) { }
ngOnInit() {
this._myserve.currentprotData.subscribe(currentprotData=>{
this.message=currentprotData
})
}
ngAfterViewInit(): void {
console.log(this.message)
}
ngOnDestroy(){
}
}
Мой сервис может получать данные, но приемник всегда возвращает ноль.Мне любопытно узнать, что здесь происходит не так!Я очень ценю вашу помощь и советы!
Обновлено Как Деборак предложила включить часть проекта в stackblitz!Вы можете проверить, как я пытаюсь построить этот проект!
stackblitz