question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:open
  • Created 5 years ago
  • Reactions:24
  • Comments:22 (12 by maintainers)

github_iconTop GitHub Comments

16reactions
ALEXOTANOcommented, Aug 8, 2019

Done, the thing is to inject ContentObserver, create an observable using it and subscribe to it.

Using above example:

@Component({
  selector: 'my-selector',
  template: `<div class="wraper" #element>
                       <ng-content></ng-content>
                   </div>`,
  styleUrls: ['./my-selector.component.css']
})
export class my-selectorComponent implements AfterViewInit, OnDestroy {

  @ViewChild('element') element: ElementRef;
  dataChanges:Subscription = null;

  constructor(
    private obs: ContentObserver
  ) { }
  ngAfterViewInit() {

    this.dataChanges = this.obs.observe(this.element.nativeElement)
                                    .subscribe((event: MutationRecord[]) => {
      console.log('Change Detected.', event);

    });
    this.started = true;
  }
  ngOnDestroy(){
    this.dataChanges.unsubscribe();
  }
}

This is the way to do it since 6+, hope helps some one that comes around in future.

9reactions
cesco69commented, Mar 19, 2019

Add the ability to detect “ng-content” changes

Would you care to elaborate on the practical use-case you are trying to solve with this new feature?

@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:

import { AfterContentChecked, AfterContentInit, Component, ElementRef, EventEmitter, Output, ViewChild } from "@angular/core";

@Component({
    selector: "my-comp",
    template: "<div #content><ng-content></ng-content></div>",
})
export class MyComponent implements AfterContentInit, AfterContentChecked {    
    @ViewChild("content") contentWrapper: ElementRef;
    content = "";
    @Output() public readonly contentChanged = new EventEmitter<string>();

    ngAfterContentInit(): void {
        this.content = this.contentWrapper.nativeElement.innerHTML;
        this.contentChanged.emit(this.content);
    }
    ngAfterContentChecked(): void {
        const c = this.contentWrapper.nativeElement.innerHTML;
        if (c !== this.content) {
            this.content = c;
            this.contentChanged.emit(this.content);
        }
    }
}

demo https://stackblitz.com/edit/angular-lxfzfw

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found