Skip to content

Commit 114d03f

Browse files
jelbournthePunderWoman
authored andcommitted
docs: update OnChanges API reference w/ signal input
We had some reports of LLMs claiming that `ngOnChanges` does not include changes for signal-based inputs. This is wrong (and the LLMs just made it up), but we can update the docs at least to demonstrate `ngOnChanges` with signal-based inputs. Previously, the API reference used a real test as a code example, but updating the test to a signal-based input is more involved than this change needs to call for. (cherry picked from commit 871fa1b)
1 parent bf5db2b commit 114d03f

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

packages/core/src/change_detection/lifecycle_hooks.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {SimpleChanges} from './simple_change';
99

1010
/**
1111
* @description
12-
* A lifecycle hook that is called when any data-bound property of a directive changes.
12+
* A lifecycle hook that is called when the value of one or more component or directive inputs
13+
* change. This includes both signal-based and decorator-based inputs.
1314
* Define an `ngOnChanges()` method to handle the changes.
1415
*
1516
* @see {@link DoCheck}
@@ -18,9 +19,19 @@ import {SimpleChanges} from './simple_change';
1819
*
1920
* @usageNotes
2021
* The following snippet shows how a component can implement this interface to
21-
* define an on-changes handler for an input property.
22-
*
23-
* {@example core/ts/metadata/lifecycle_hooks_spec.ts region='OnChanges'}
22+
* define an on-changes handler for an input property. While you should prefer
23+
* `computed` and `effect` when working with signal-based inputs, the `ngOnChanges`
24+
* method does include value changes for signal-based inputs.
25+
*
26+
* ```ts
27+
* @Component({ ... })
28+
* export class UserProfile implements OnChanges {
29+
* userId = input<number>(0);
30+
* ngOnChanges(changes: SimpleChanges<UserProfile>) {
31+
* // changes.userId contains the old and new value.
32+
* }
33+
* }
34+
* ```
2435
*
2536
* @publicApi
2637
*/

0 commit comments

Comments
 (0)