Comments (5)
Referenced in Ionic: ionic-team/ionic-framework#29444
from angular.
Hello. We found the same issue in a new project inside our company. It looks like the problem is really the changeDetectorRef.detach
. This simple component isn't shown in effect
too, if you add the same directive:
import { ChangeDetectionStrategy, ChangeDetectorRef, Component } from '@angular/core';
@Component({
selector: 'app-test-button',
standalone: true,
imports: [],
templateUrl: './test-button.component.html',
styleUrl: './test-button.component.css',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class TestButtonComponent {
constructor(changeRef: ChangeDetectorRef) {
changeRef.detach();
}
}
Component and directive:
@Directive({
selector: '[test]',
standalone: true,
})
export class TestDirective {
test = input.required<string>();
test2 = computed(() => {
return this.test() + '2';
});
constructor() {
effect(() => {
console.log('>> EFFECT', this.test2());
});
}
}
.........
<app-test-button [test]="inputText">
Button with Directive
</app-test-button>
from angular.
The root cause is the injection of the ChangeDetectorRef
in directives. When the directive is applied to a component, the CDR injected in the directive is the components CDR.
The effect is scheduled to run first when the component is init and uses the CDR to do that. In this case, the component is never init (because of running detach
in the constructor) so the effect is never started.
A workaround for this would be to let the component finish its initialization and detach only in the ngAfterViewInit
hook. This way the effect will
from angular.
Here is a minimal repro: https://stackblitz.com/edit/stackblitz-starters-7zd1fv?file=src%2Fmain.ts
As @JeanMeche pointed out, the root cause here is a component detaching from the change detection tree. Having said this I do recognize that this is very non-obvious - this is one of the cases we want to look into before stabilizing effects
API.
from angular.
I've created #55808 to capture the root cause without any external dependencies. Let's continue the discussion in there.
Duplicate of #55808
from angular.
Related Issues (20)
- Scope of template local variables in Angular @if() block seems wrong HOT 3
- ngsp is not working properly with control flow syntax HOT 1
- @for loop with string identifier throw exception after changing HOT 10
- Improve SEO of angular.dev HOT 1
- Bounds attributes [attr.xxx] do not trigger directives HOT 5
- Reactivity: effects won't execute if created in a component that is detached from a change detection tree HOT 1
- Recursion bug in Angular (child in defer block) HOT 5
- No error showing in HTML for invalid Angular IF statement HOT 3
- Is there a way of renaming input component bindings HOT 2
- Pipes API Documentation Link is Invalid HOT 1
- Angular localstorage not working with SSR inside Authguard HOT 1
- Subject: Feature Request: Restore Reset Functionality for contentChildren Signal API HOT 3
- Broken zone-patch-rxjs in Zone.js 0.14.5
- Allow Custom CSS Variables in [ngStyle] and Renderer2.setStyle HOT 1
- Add support for "on-site" view transitions HOT 3
- Routing stops working with no errors being shown HOT 3
- Angular 17 fakeAsync tasks not running until test is cleaned up with 'resetFakeAsyncZone()' HOT 1
- Request with HttpClient not displayed in browser inspector but works HOT 7
- platform-browser.mjs _getStyleElement function under SharedStylesHost class trigger CSP error HOT 2
- Not found images from tutorial HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from angular.