我有角分量,第一个分量使用第二个作为指令。它们应该共享相同的模型对象,该模型在第一个组件中初始化。如何将该模型传递给第二个组件?
发布于 2015-12-04 12:27:13
组件2,指令组件可以定义一个输入属性(类型记录中的@input注释)。组件1可以从模板将该属性传递给指令组件。
看到这个,所以回答如何在Angular2中实现主组件和细节组件之间的通信?
以及如何将输入传递给子组件。在您的情况下,这是指令。
发布于 2017-04-05 07:18:15
您还可以使用setter将数据存储在服务中,并通过getter获取数据。
import { Injectable } from '@angular/core';
@Injectable()
export class StorageService {
public scope: Array<any> | boolean = false;
constructor() {
}
public getScope(): Array<any> | boolean {
return this.scope;
}
public setScope(scope: any): void {
this.scope = scope;
}
}发布于 2019-06-26 07:18:13
从组分
import { Component, OnInit, ViewChild} from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { dataService } from "src/app/service/data.service";
@Component( {
selector: 'app-sideWidget',
templateUrl: './sideWidget.html',
styleUrls: ['./linked-widget.component.css']
} )
export class sideWidget{
TableColumnNames: object[];
SelectedtableName: string = "patient";
constructor( private LWTableColumnNames: dataService ) {
}
ngOnInit() {
this.http.post( 'getColumns', this.SelectedtableName )
.subscribe(
( data: object[] ) => {
this.TableColumnNames = data;
this.LWTableColumnNames.refLWTableColumnNames = this.TableColumnNames; //this line of code will pass the value through data service
} );
}
}
DataService
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
@Injectable()
export class dataService {
refLWTableColumnNames: object;//creating an object for the data
}
转到组件
import { Component, OnInit } from '@angular/core';
import { dataService } from "src/app/service/data.service";
@Component( {
selector: 'app-linked-widget',
templateUrl: './linked-widget.component.html',
styleUrls: ['./linked-widget.component.css']
} )
export class LinkedWidgetComponent implements OnInit {
constructor(private LWTableColumnNames: dataService) { }
ngOnInit() {
console.log(this.LWTableColumnNames.refLWTableColumnNames);
}
createTable(){
console.log(this.LWTableColumnNames.refLWTableColumnNames);// calling the object from another component
}
}
https://stackoverflow.com/questions/34088209
复制相似问题