Detect ng-content changes
See original GitHub issue🚀 feature request
Relevant Package
This feature request is for @angular/core
Description
Add the ability to detect “ng-content” changes into OnChanges method like @Input.
Describe the solution you’d like
SimpleChanges can be expose a special prop. ngContent that contains the old and the new value of ng-content, eg.:
@Component({selector: 'my-cmp', template: `<ng-content></ng-content>`})
class MyComponent implements OnChanges {
ngOnChanges(changes: SimpleChanges) {
console.log(changes.ngContent);
}
}
Describe alternatives you’ve considered
Add a new lifecycle hook that is called when ng-content change, eg.:
@Component({selector: 'my-cmp', template: `<ng-content></ng-content>`})
class MyComponent implements OnNgContentChanges {
ngOnNgContentChanges() {
console.log('ng-content change!');
}
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:24
- Comments:22 (12 by maintainers)
Top Results From Across the Web
angular2 detect changes in ng-content - Stack Overflow
My solution for a project is to monitor the content's innerHTML. This example is from a tooltip that shows template content ...
Read more >Angular ng-content and Content Projection: A Complete Guide ...
The ng-content core directive allows for component designs where certain internal details of the template are not hidden inside the component ...
Read more >Lifecycle hooks - Angular
Demonstrates how Angular calls the ngOnChanges() hook every time one of the component input properties changes, and shows how to interpret the changes...
Read more >The Last Guide For Angular Change Detection You'll Ever Need
In the default change detection strategy, Angular will run the change detector any time @Input() data is changed or modified. Using the OnPush...
Read more >Simplifying Angular Change Detection - Telerik
In the Default strategy, whenever any data to @Input() decorated properties are changed, Angular runs the change detector to update the view. In ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Done, the thing is to inject ContentObserver, create an observable using it and subscribe to it.
Using above example:
This is the way to do it since 6+, hope helps some one that comes around in future.
@pkozlowski-opensource i have made a wysiwyg editor component that allow the live edit of the content of ng-content and i need to detect changes.
Now, the work-around is a bit tricky:
demo https://stackblitz.com/edit/angular-lxfzfw