如何将选定值传递给其他组件角度2

时间:2017-11-01 09:15:11

标签: angular typescript angular2-forms

我正在尝试传递从其中一个选项中选择的值。我使用ngModel来保存值,但我无法弄清楚如何将其传递给其他组件。

Component1.ts

  <select [(ngModel)]="selectedElement.id">
    <option *ngFor="let type of types" [ngValue]="type.id"> {{type.Name}}</option>
  </select>
  @Input() selectedElement:any= {id:2,Name:'abdfsdgsc'};
  types:any[]=[
    {id:1,Name:'abc'},
    {id:2,Name:'abdfsdgsc'}
];

component2.ts

constructor() {
  if(this.selectedElement.id==1){
     themeConfig.config();
  }else {
     themeConfig.theme();
  }
}

2 个答案:

答案 0 :(得分:0)

您需要使用eventemitter输出值,并在第二个组件输入中

答案 1 :(得分:0)

只需在组件2中包含您要提供的值作为输入,并检查ngOnInit生命周期方法

HTML

Sub Differenz1()

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

'Variabledefinition
Dim i As Long 'Index
Dim j As Long 'Index
Dim k As Long 'Index
Dim ArrayA As Variant 'Array
Dim ArrayB As Variant 'Array
Dim ArrayC(71, 25) As Variant  'Array
Dim myFile1 As String 'Workbookname
Dim myFile2 As String 'Workbookname
Dim wb1 As String 'Workbookname
Dim wb2 As String 'Workbookname
Dim WS_Count1 As Integer 'Count Worksheets
Dim WS_Count2 As Integer 'Count Worksheets
Dim arrays1 As String 'Dimension
Dim arrays2 As String 'Dimension


'Change the actual path

ChDrive "O:\"
ChDir "O:..."


myFile1 = Application.GetOpenFilename
Workbooks.Open Filename:=myFile1, ReadOnly:=True, IgnoreReadOnlyRecommended:=True
wb1 = ActiveWorkbook.Name
WS_Count1 = ActiveWorkbook.Worksheets.Count



myFile2 = Application.GetOpenFilename
Workbooks.Open Filename:=myFile2, ReadOnly:=True, IgnoreReadOnlyRecommended:=True
wb2 = ActiveWorkbook.Name
WS_Count2 = ActiveWorkbook.Worksheets.Count



For k = 1 To WS_Count1

ArrayA = Workbooks(wb1).Worksheets(k).Range("F5:Y75").Value
ArrayB = Workbooks(wb2).Worksheets(k).Range("F5:Y75").Value
    For i = LBound(ArrayA, 1) To UBound(ArrayA, 1)
        For j = LBound(ArrayA, 2) To UBound(ArrayA, 2)
            If Not IsError(ArrayA(i, j)) And Not IsError(ArrayB(i, j)) Then ArrayC(i, j) = ArrayA(i, j) - ArrayB(i, j)
        Next j
    Next i

    ThisWorkbook.Worksheets(k + 1).Range("F5:Y75").Value = ArrayC

Next k


Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub

TS

<component2 [selectedID]="selectedElement.id"></component2>