Advanced Angular @HostListener-记录父组件(来自子组件)中所有元素的点击

时间:2019-05-24 20:17:36

标签: javascript angular events event-handling

我想在其中保留子组件的逻辑。但是,事件本身是从其父组件向下单击时发生的。

    

child.component.ts

C:\User\Kiyoshi\Documents\011.201-2016-CTE-AditivoDeConvenio-01-R00-FFF.docx

我尝试了很多事情来遍历节点列表并比较click event.target以查看该点击是在011.201 2016 CTE AditivoDeConvenio 01 R00 FFF 内部还是外部,但是我似乎无法遍历nodeList并且没有了解原因。

我在repro StackBlitz上也尝试了许多其他操作。

也许有更好的方法来监视此组件父对象外部/内部的点击?另外,我不想将单击逻辑移到父级,除非必须存在,否则该逻辑不属于父级。

已修复:StackBlitz已更新,其中包括我对所有使用elementRef遍历DOM的研究。 此外,@ Ritchie可以正确遍历nodeList并使其正常工作。

1 个答案:

答案 0 :(得分:1)

您可以使用Sub tgr() Dim wb As Workbook Dim wsMaster As Worksheet Dim wsRPT As Worksheet Dim rFound As Range Dim aCandidateIDs As Variant Dim vID As Variant Set wb = ActiveWorkbook Set wsMaster = wb.Worksheets("MasterSheet") Set wsRPT = wb.Worksheets("RPT - Shelf Age") With wsRPT.Range("D3", wsRPT.Cells(wsRPT.Rows.Count, "D").End(xlUp)) If .Row < 3 Then Exit Sub 'No data If .Cells.Count = 1 Then ReDim aCandidateIDs(1 To 1, 1 To 1) aCandidateIDs(1, 1) = .Value Else aCandidateIDs = .Value End If End With With wsMaster.Range("D1:F" & wsMaster.Cells(wsMaster.Rows.Count, "D").End(xlUp).Row) For Each vID In aCandidateIDs .AutoFilter 1, vID 'Filter column D for the ID .AutoFilter 3, "Scheduled for Delivery", xlOr, "Equipment Deployed" 'Filter column F for the two other strings 'Check if there are any results On Error Resume Next Set rFound = .Offset(1).SpecialCells(xlCellTypeVisible).Cells(1) 'This assumes you only want the first match found. For all matches, leave off the .Cells(1) On Error GoTo 0 .AutoFilter 'Remove the filter If Not rFound Is Nothing Then 'Found a match, do something with it here 'Because we only returned the first match, no need to iterate over rFound because it will only be 1 cell MsgBox "Candidate ID: " & vID & Chr(10) & "Match found on MasterSheet at row: " & rFound.Row End If Next vID End With End Sub 的{​​{1}}属性来检查单击了哪个组件,并将其与父节点名称进行比较。

target

Stackblitz Example