I have an Angular component inheriting a base component.
@Component({
selector: "my-baseclass-component",
template: `
<div style="border:1px solid red;padding:10px">
counter value (check console to see it gets incremented) : {{ counter }}
<ng-content select="[body]"></ng-content>
<br />
</div>
`
})
export class BaseClassComponent {
public counter = 0;
constructor() {}
}
I just want to increment a counter in the base component from the inherited component when clicking on a button.
@Component({
selector: 'my-inherited-component',
template: `<my-baseclass-component #myBaseclass>
<div style="border:1px solid black" body>Inherited Component Content
<button (click)="click()">toggle</button>
</div>
</my-baseclass-component>`
})
export class InheritedComponent extends BaseClassComponent {
click(){
this.counter++;
console.log('clicked:' + this.counter);
}
}
The counter variable is displayed in the base component template using the curly brackets notation {{counter}}
but it is never refreshed although the value itself is incremented correctly.
I have reproduced the issue in a stackblitz :
https://stackblitz.com/edit/angular-ivy-sssxvt?file=src/app/baseclass.component.ts
What am I doing wrong ?
question from:
https://stackoverflow.com/questions/66054742/inherited-component-fails-to-update-property-in-base-component 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…