I'm trying to use data received through an @Output to trigger if statements inside my component. For testing purposes I have this in the template
{{selectedAnswer | json}}
When I select answers in the child component this binding works in the parent component showing the data is reaching the variable selectedAnswer. Yet when I do console.log(this.selectedAnswer) nothing logs.
I've also been trying to get it to update to a variable to run the if conditions. When the page first loads it picks up the value of the first radio button when I don't have any selected by default. When I click through them the value updates in the test binding but nothing changes in the console.logs.
This is what's going on in the parent component.
//data coming in from @Output in child component
public selectedI(selected){ this.selectedAnswer = selected };
//the variable I'm passing it into for use.
selectedAnswer: any;
ngOnChanges() {
//my way so far of attempting to get the value I want when a selection is made.
var trigger = this.selectedAnswer? this.selectedAnswer.value1: '';
console.log(this.selectedAnswer);
}
Template
<multiple-choice-radio *ngIf="expbTrigger"
[question] = "question01"
(selectedO) = "selectedI($event)"
></multiple-choice-radio>
The only thing different in calling it here vs. calling it in the binding is the binding has a |json pipe added to it. Am I suppose to be doing the same thing somehow inside the expression? The way the data is being collected in the child component is like this
@Output() selectedO: EventEmitter<any> = new EventEmitter();
//variable for storing values from (click) event
selected = {value1: '', value2: ''};
//(click) event that gets values from template and passes them into "selected"
getSelected(ans) {
this.selected = { value1: ans.id, value2: ans.answer };
}
//(click) event that sends @Output data to parent component.
public sendAnswer = (): void => {
this.selectedO.emit(this.selected);
}
Template
<input type="radio"
[attr.name] = "quesForm.value.name"
[attr.id] = "ans.id"
[attr.value] = "ans.answer"
(click) = "getSelected(ans)"
(click) = "sendAnswer()"
hidden
/>
I've personally been feeling the way I'm doing all this is a bit bulky so if you know of a cleaner way to do this please share. Other than that, how can I get this to log in the console as well as be accessible for further use in triggering logic upon updates?