如何在angular 4中的2个兄弟组件之间进行通信?
我想在一个组件中勾选复选框,并在选中复选框时显示不同组件中的数据 组成部分1如何在angular 4中的2个兄弟组件之间进行通信?,angular,angular-forms,Angular,Angular Forms,我想在一个组件中勾选复选框,并在选中复选框时显示不同组件中的数据 组成部分1 <div class="col-md-6" *ngFor="let mapLegend of mapLegends"> <label class="checkbox"> <input type="checkbox" name="mapLegend" value="{{mapLegend.name
<div class="col-md-6" *ngFor="let mapLegend of mapLegends">
<label class="checkbox">
<input type="checkbox"
name="mapLegend"
value="{{mapLegend.name}}"
[(ngModel)]="mapLegend.checked"/>
<span class="checkbox__input"></span>
<span class="checkbox__label"> {{mapLegend.name}}</span>
</label>
</div>
构成部分2
如果选中mapLegend.则需要数据有几种方法。根据您的要求,我认为您希望观察两个组件中所做的更改。所以我建议你们去研究这个课题 在服务中创建主题。现在,订阅组件中的变量,您要在其中侦听已发生的任何更改
export class SomeService{
public mySubject = new Subject();
choice: []<any>;
insertCheckBoxValue(val){
this.choice.push(val);
this.mySubject.next('the value you want to transmit to all Subscribers'); // Let's say: Yoda
}
}
export class Component1{
constructor(private svc: SomeService){}
this.svc.mySubject().subscribe( val => console.log(val)) // this will listen to the changes of Comp2 : Yoda
checkBoxClicked(someVal){
this.svc.insertCheckBoxValue(someVal);
}
}
export class Component2{
constructor(private svc: SomeService){}
this.svc.mySubject().subscribe( val => console.log(val)) // this will listen to the changes of Comp1 : Yoda
checkBoxClicked(someVal){
this.svc.insertCheckBoxValue(someVal);
}
}
如果要共享数据,请在SomeService文件中创建一个变量,并在触发.next时获取其值。实际上,您可以从.nextyYour_值中共享一个公共值,然后在另一个组件中订阅它
&
调整上述代码以满足您的需求并提高效率。必须对其进行改进,以满足您的特定需求有几种方法可以做到这一点。根据您的要求,我认为您希望观察两个组件中所做的更改。所以我建议你们去研究这个课题 在服务中创建主题。现在,订阅组件中的变量,您要在其中侦听已发生的任何更改
export class SomeService{
public mySubject = new Subject();
choice: []<any>;
insertCheckBoxValue(val){
this.choice.push(val);
this.mySubject.next('the value you want to transmit to all Subscribers'); // Let's say: Yoda
}
}
export class Component1{
constructor(private svc: SomeService){}
this.svc.mySubject().subscribe( val => console.log(val)) // this will listen to the changes of Comp2 : Yoda
checkBoxClicked(someVal){
this.svc.insertCheckBoxValue(someVal);
}
}
export class Component2{
constructor(private svc: SomeService){}
this.svc.mySubject().subscribe( val => console.log(val)) // this will listen to the changes of Comp1 : Yoda
checkBoxClicked(someVal){
this.svc.insertCheckBoxValue(someVal);
}
}
如果要共享数据,请在SomeService文件中创建一个变量,并在触发.next时获取其值。实际上,您可以从.nextyYour_值中共享一个公共值,然后在另一个组件中订阅它
&
调整上述代码以满足您的需求并提高效率。必须对其进行改进以满足您的特定需求但@Shashank Vivek仍然是进行跨组件通信的最通用方式。如果您需要的只是您所描述的: 如果我的复选框为true,则使用我的模型作为参数初始化childComponent,然后可以执行以下操作: 组成部分:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
mapLegends: Array<{
checked: boolean,
name: string
}>;
constructor() {
this.mapLegends = [
{
checked: false,
name: 'Check 1'
},
{
checked: false,
name: 'Check 2'
}
];
}
onCheckboxChange(index: number) {
console.log(index);
console.log(this.mapLegends[index]);
}
}
或在component.html中:
不过@Shashank Vivek是进行跨组件通信的最通用的方式。如果您需要的只是您所描述的: 如果我的复选框为true,则使用我的模型作为参数初始化childComponent,然后可以执行以下操作: 组成部分:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
mapLegends: Array<{
checked: boolean,
name: string
}>;
constructor() {
this.mapLegends = [
{
checked: false,
name: 'Check 1'
},
{
checked: false,
name: 'Check 2'
}
];
}
onCheckboxChange(index: number) {
console.log(index);
console.log(this.mapLegends[index]);
}
}
或在component.html中:
您可以使用共享服务并从注入该服务的任何组件发送通知 考虑以下几点: checkbox.service.ts sender.component.ts 要为复选框设置新状态,只需使用:
changeCheckboxState(){
this.cs.changeCheckboxState(this.mapLegendCheckbox);
}
注
不要忘记在同一模块的提供数组中添加服务,在声明数组中添加组件,这样就不会出现任何错误 您可以使用共享服务并从注入该服务的任何组件发送通知 考虑以下几点: checkbox.service.ts sender.component.ts 要为复选框设置新状态,只需使用:
changeCheckboxState(){
this.cs.changeCheckboxState(this.mapLegendCheckbox);
}
注
不要忘记在同一模块的提供数组中添加服务,在声明数组中添加组件,这样就不会出现任何错误 你能给我们看第二个组件调用吗?最简单的方法是在两个组件中使用一个服务并使用它来共享数据,否则你可以使用@Input和@Output并通过父级传递数据。阅读我如何共享[ngModel]beetween 2组件?您能向我们展示第二个组件调用吗?最简单的方法是在两个组件中使用服务并使用它来共享数据,否则您可以使用@Input和@Output并通过父级传递数据。阅读如何共享[ngModel]beetween 2组件?我想在不在子组件或父组件中的同级组件之间共享此数据。我不确定是否理解,您能否根据我的代码添加简单的示例库?我不想在应用组件中导入hello组件。您最初的问题要求2个组件,您想在同一个组件上执行所有测试吗?我们可以将其添加到服务中吗?我想在不在子组件或父组件中的兄弟组件之间共享此数据。我不确定是否理解,您可以根据我的代码添加简单的示例吗?我不想在应用组件中导入hello组件。您最初的问题要求两个组件,你想在同一台机器上完成所有训练吗?我们可以在服务中添加这个吗?你可以制作这个演示吗?@cssmatter.com:检查我添加的演示吗?你可以制作这个演示吗?@cssmatter.com:检查我添加的演示吗
constructor(private cs: CheckboxService) {
this.cs.checkboxObservable
.subscribe((checkboxState) => {
// getting the value
console.log(checkboxState);
});
}
changeCheckboxState(){
this.cs.changeCheckboxState(this.mapLegendCheckbox);
}