You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: adev/src/content/guide/components/inputs.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -230,7 +230,7 @@ export class MediaControls {
230
230
231
231
In the above example, the `CustomSlider` can write values into its `value` model input, which then propagates those values back to the `volume` signal in `MediaControls`. This binding keeps the values of `value` and `volume` in sync. Notice that the binding passes the `volume` signal instance, not the _value_ of the signal.
232
232
233
-
In other respects, model inputs work similarly to standard inputs. You can read the value by calling the signal function, including in reactive contexts like `computed` and `effect`.
233
+
In other respects, model inputs work similarly to standard inputs. You can read the value by calling the signal function, including in [reactive contexts](guide/signals#reactive-contexts) like `computed` and `effect`.
234
234
235
235
See [Two-way binding](guide/templates/two-way-binding) for more details on two-way binding in templates.
Copy file name to clipboardExpand all lines: adev/src/content/guide/signals/overview.md
+62-35Lines changed: 62 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -89,6 +89,68 @@ If you set `showCount` to `true` and then read `conditionalCount` again, the der
89
89
90
90
Note that dependencies can be removed during a derivation as well as added. If you later set `showCount` back to `false`, then `count` will no longer be considered a dependency of `conditionalCount`.
91
91
92
+
## Reactive contexts
93
+
94
+
A **reactive context** is a runtime state where Angular monitors signal reads to establish a dependency. The code reading the signal is the _consumer_, and the signal being read is the _producer_.
95
+
96
+
Angular automatically enters a reactive context when:
97
+
98
+
- Executing an `effect`, `afterRenderEffect` callback.
99
+
- Evaluating a `computed` signal.
100
+
- Evaluating a `linkedSignal`.
101
+
- Evaluating a `resource`'s params or loader function.
102
+
- Rendering a component template (including bindings in the [host property](guide/components/host-elements#binding-to-the-host-element)).
103
+
104
+
During these operations, Angular creates a _live_ connection. If a tracked signal changes, Angular will _eventually_ re-run the consumer.
105
+
106
+
### Asserts the reactive context
107
+
108
+
Angular provides the `assertNotInReactiveContext` helper function to assert that code is not executing within a reactive context. Pass a reference to the calling function so the error message points to the correct API entry point if the assertion fails. This produces a clearer, more actionable error message than a generic reactive context error.
Rarely, you may want to execute code which may read signals within a reactive function such as `computed` or `effect`_without_ creating a dependency.
122
+
123
+
For example, suppose that when `currentUser` changes, the value of a `counter` should be logged. You could create an `effect` which reads both signals:
124
+
125
+
```ts
126
+
effect(() => {
127
+
console.log(`User set to ${currentUser()} and the counter is ${counter()}`);
128
+
});
129
+
```
130
+
131
+
This example will log a message when _either_`currentUser` or `counter` changes. However, if the effect should only run when `currentUser` changes, then the read of `counter` is only incidental and changes to `counter` shouldn't log a new message.
132
+
133
+
You can prevent a signal read from being tracked by calling its getter with `untracked`:
134
+
135
+
```ts
136
+
effect(() => {
137
+
console.log(`User set to ${currentUser()} and the counter is ${untracked(counter)}`);
138
+
});
139
+
```
140
+
141
+
`untracked` is also useful when an effect needs to invoke some external code which shouldn't be treated as a dependency:
142
+
143
+
```ts
144
+
effect(() => {
145
+
const user =currentUser();
146
+
untracked(() => {
147
+
// If the `loggingService` reads signals, they won't be counted as
148
+
// dependencies of this effect.
149
+
this.loggingService.log(`User set to ${user}`);
150
+
});
151
+
});
152
+
```
153
+
92
154
## Advanced derivations
93
155
94
156
While `computed` handles simple readonly derivations, you might find youself needing a writable state that is dependant on other signals.
Rarely, you may want to execute code which may read signals within a reactive function such as `computed` or `effect`_without_ creating a dependency.
154
-
155
-
For example, suppose that when `currentUser` changes, the value of a `counter` should be logged. You could create an `effect` which reads both signals:
156
-
157
-
```ts
158
-
effect(() => {
159
-
console.log(`User set to ${currentUser()} and the counter is ${counter()}`);
160
-
});
161
-
```
162
-
163
-
This example will log a message when _either_`currentUser` or `counter` changes. However, if the effect should only run when `currentUser` changes, then the read of `counter` is only incidental and changes to `counter` shouldn't log a new message.
164
-
165
-
You can prevent a signal read from being tracked by calling its getter with `untracked`:
166
-
167
-
```ts
168
-
effect(() => {
169
-
console.log(`User set to ${currentUser()} and the counter is ${untracked(counter)}`);
170
-
});
171
-
```
172
-
173
-
`untracked` is also useful when an effect needs to invoke some external code which shouldn't be treated as a dependency:
174
-
175
-
```ts
176
-
effect(() => {
177
-
const user =currentUser();
178
-
untracked(() => {
179
-
// If the `loggingService` reads signals, they won't be counted as
180
-
// dependencies of this effect.
181
-
this.loggingService.log(`User set to ${user}`);
182
-
});
183
-
});
184
-
```
185
-
186
213
## Using signals with RxJS
187
214
188
215
See [RxJS interop with Angular signals](ecosystem/rxjs-interop) for details on interoperability between signals and RxJS.
0 commit comments