【问题标题】:how to pass content one Component to another Component using master Component in angular 2如何使用角度 2 中的主组件将内容一个组件传递给另一个组件
【发布时间】:2017-05-19 12:56:01
【问题描述】:

app.module.ts

@NgModule({
  imports:      [ BrowserModule, FormsModule, HttpModule,MyDatePickerModule ],
 declarations: [ AppComponent,HeaderComponent,
              ContentComponent,ActionComponent ,
              FacultyComponent,StudentComponent,
              filterPipe
            ],                
  providers:    [ DataService ], 
  bootstrap:    [ AppComponent ]
 })
  export class AppModule { }

app.component.ts

@Component({
  selector: 'my-app',
  templateUrl: `./app/app.components.html`,
})
export class AppComponent  { }

app.components.html

     <my-header></my-header>
     <my-content></my-content>

header.components.ts

@Component({
   selector: 'my-header',
  templateUrl: `./app/header/header.components.html`
 })
 export class HeaderComponent  {
   batchObj: Task;
   myDatePickerOptions: any;  
   constructor(private dataService: DataService) { }   
}

header.components.html

      Batch : <select  [(ngModel)]="sel_batch"  > <option >Select Batch</option>
                   <option *ngFor="let item of  batchObj | filterPipe: [];   ">{{item.batch}}</option>
            </select>
    Term : <select  [(ngModel)]="sel_term"> <option>Select Term</option>
     <option *ngFor="let item of batchObj | filterPipe: ['batch', sel_batch,'term'];   ">{{item.term}}</option>
     </select>           
    Section : <select  [(ngModel)]="sel_section"> <option>Select Section</option> 
     <option *ngFor="let item of batchObj | filterPipe: ['batch', sel_batch,'term',sel_term,'section'];">{{item.section}}</option>
    </select> 

content.components.ts

@Component({
 selector: 'my-content',
 templateUrl: `./app/content/content.components.html`    
 })
  export class ContentComponent  { 
 }

这个 my-header my-content 两个自定义标签都存在于我的主 app.component.ts 文件中

所以我想将three select box 值从my-header 传递到my-content 页面

1) how to do it?

2) 我使用了三个选择框,每个选择框值在调用上一个时出现,因为我使用 ng-model 传递值,但我看到了一些示例,其中将值从一个组件传递到另一个使用,例如 #sel_batch 而不是 @ 987654335@ 但随后filterPipe: ['batch', sel_batch,'term'] 不起作用 哪种语法适合我的情况

【问题讨论】:

    标签: angular


    【解决方案1】:

    您必须创建一个在您的HeaderComponentContentComponent 之间共享的公共服务,并且使用此服务您可以在HeaderComponentContentComponent 之间进行通信。检查Component communication via a service documentation 和我对parent-child or sibling component comunication 的回答。

    在您完成公共服务后,您必须在选择下拉值更改时从HeaderComponent 发出事件,这需要已经是subscribe ContentComponent,以便您可以根据下拉值更改执行您的功能

    【讨论】:

    • {option: 'call_child', value: 'From child'} 这部分是做什么的?如何在这里传递我的选择批次
    • notifyOther 中传递{option: 'call_child', value: 'From child'},您将在subscribe 内部ContentComponent 内部接收res。因此,您可以将要发送的值从notifyOther 传递给ContentComponent
    • 请考虑这个例子 [(ngModel)]="sel_batch" 上面的选择框如何传入
    • 尝试通过传递{option: 'batch_changed', value: this.sel_batch} 来检查批次和{option: 'term_changed', value: this.sel_term} 来检查术语值的变化,并在subscribe 内部比较res.option 的值与term_changedbatch_changed 而不是@987654343 @ 执行您的操作。
    • 好的,我会尝试希望这对我来说还有一个问题,大多数视频使用指令都为此提供了一个问题,但它的显示已被弃用。任何其他替代方案
    猜你喜欢
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 2019-10-27
    • 2019-10-23
    相关资源
    最近更新 更多