如何使用Angular 8将按钮单击时的数据从一个组件传递到另一个组件?
我正在尝试将数据从一个组件传递到另一个组件,我希望它在单击按钮时发生。不知道该如何设置。我已经用@Output()和@Input()标记了我的属性,并使用appproperties设置设置了传递,但希望在单击按钮时发生这种情况 在我的question.component.html中,这是我创建的按钮:如何使用Angular 8将按钮单击时的数据从一个组件传递到另一个组件?,angular,Angular,我正在尝试将数据从一个组件传递到另一个组件,我希望它在单击按钮时发生。不知道该如何设置。我已经用@Output()和@Input()标记了我的属性,并使用appproperties设置设置了传递,但希望在单击按钮时发生这种情况 在我的question.component.html中,这是我创建的按钮: <div class="viewResultsNav" *ngIf="question && question.questionId === totalQuesti
<div class="viewResultsNav" *ngIf="question && question.questionId ===
totalQuestions">
<button type="button" (click)="navigateToResults();">
View Your Results
</button>
</div>
查看您的结果
我有一个模板,用于传递应用程序结果:
<ng-template #quiz_results>
<app-results
[allQuestions]="allQuestions"
[totalQuestions]="totalQuestions"
[totalQuestionsAttempted]="totalQuestionsAttempted"
[correctAnswers]="correctAnswers"
[progressValue]="progressValue"
[percentage]="percentage">
</app-results>
</ng-template>
在子组件中,所有这些属性都用@Input()标记,因此我希望这些属性通过按钮传递到组件中。现在,如果我单击按钮,它将导航到ResultComponent,但不会传递任何数据。是否可以通过路由器传递数据,或者可以通过按钮提供ng模板测验结果?尝试以下方法: app-parent.ts:
receivedChildMessage: string;
getMessage(message: string) {
this.receivedChildMessage = message;
}
app-parent.html
<app-child (messageToEmit)="getMessage($event)"></app-child>
app-child.ts:
@Output() messageToEmit = new EventEmitter<string>();
sendMessageToParent(message: string) {
this.messageToEmit.emit(message)
}
@Output()messageToEmit=neweventemitter();
sendMessageToParent(消息:字符串){
this.messageToEmit.emit(message)
}
app-child.html:
<button (click)="sendMessageToParent(messageToSendC)">Send to Parent</button>
发送到父级
请通过添加一些代码来帮助我们,以便我们可以帮助您。请参见上文,我编辑了我的问题。