typescript - angular signal is not recomputing - Stack Overflow
I have this code in my constructor
public constructor() {
this.dropdown = signal([
{
name: 'Normal Text',
tag: 'p',
selected: true
},
{
name: 'Heading 1',
tag: 'h1',
selected: false,
},
{
name: 'Heading 2',
tag: 'h2',
selected: false
},
{
name: 'Heading 3',
tag: 'h3',
selected: false
},
{
name: 'Heading 4',
tag: 'h4',
selected: false
},
{
name: 'Heading 5',
tag: 'h5',
selected: false
},
{
name: 'Heading 6',
tag: 'h6',
selected: false
}
]);
this.selectedTextType = computed(() => this.dropdown().find(x => x.selected) as DropdownOption);
}
and when an option is clicked from the dropdown this code is executed
public textTypeChange(option: DropdownOption, e: Event): void {
e.preventDefault();
this.selectedTextType().selected = false;
option.selected = true;
}
The selectedTextType
signal does not get recomputed. The dropdown option does not change in the html. What am I doing wrong?
I have this code in my constructor
public constructor() {
this.dropdown = signal([
{
name: 'Normal Text',
tag: 'p',
selected: true
},
{
name: 'Heading 1',
tag: 'h1',
selected: false,
},
{
name: 'Heading 2',
tag: 'h2',
selected: false
},
{
name: 'Heading 3',
tag: 'h3',
selected: false
},
{
name: 'Heading 4',
tag: 'h4',
selected: false
},
{
name: 'Heading 5',
tag: 'h5',
selected: false
},
{
name: 'Heading 6',
tag: 'h6',
selected: false
}
]);
this.selectedTextType = computed(() => this.dropdown().find(x => x.selected) as DropdownOption);
}
and when an option is clicked from the dropdown this code is executed
public textTypeChange(option: DropdownOption, e: Event): void {
e.preventDefault();
this.selectedTextType().selected = false;
option.selected = true;
}
The selectedTextType
signal does not get recomputed. The dropdown option does not change in the html. What am I doing wrong?
1 Answer
Reset to default 1By default, Angular signals use referential equality to track whether the value has changed. If you are storing objects or arrays in your Signal, this means that the value is treated as unchanged as long as the reference is the same. If you are updating an object inside your array, the reference remains unchanged. Angular thinks that the value is not changed and thus the template doesn't update, which is by design.
To fix it, I strongly recommend to separate the data from the selection state. For example, you can simply store the tag
of the selected value in a separate signal:
this.dropdown = signal([
{
name: "Normal Text",
tag: "p",
},
{
name: "Heading 1",
tag: "h1",
},
// ...
]);
this.selectedTextType = signal('p');
Another approach would be to install @angular/cdk
in your project and use their SelectionModel
. The code should look somewhat like the following:
this.dropdown = signal([
{
name: "Normal Text",
tag: "p",
},
{
name: "Heading 1",
tag: "h1",
},
// ...
]);
this.selectionModel = new SelectionModel(false, ['p']);
- 电脑展趋势分析 移动终端大热DIY被弱化
- 英特尔移动之战
- 恶意软件侵害苹果用户!4G更易受攻击
- spring boot - This JPQL translation to SQL doesn't make any sense - Stack Overflow
- pycharm - Why would the python code throw an exception (invalid instance in App.root), if "@property" is remov
- IMDB pagination-container for a series in python - Stack Overflow
- PowerShell not reading comment-based help section correctly - Stack Overflow
- python - Plotting a list of integer 3d values into Axes3d.voxels - Stack Overflow
- algorithm - LeetCode help , 3 letter palindrome question - medium - Stack Overflow
- python - How to explicitly define a function's domain in sympy - Stack Overflow
- visual studio code - Cant use pip install on pytorch Python 3.13.01, MacOS Sonoma 14.6.1 - Stack Overflow
- python - Azure Cognitive Vector search query and index creation - Stack Overflow
- regex - Change text block with nearest search pattern - Stack Overflow
- Composing dependent functions in Lean - Stack Overflow
- class - (Python) cant access attribute of an object unless I check nullity, why? - Stack Overflow
- postgresql - Custom stopwords dictionary in Postgres hosted on GCP Cloud SQL - Stack Overflow
- Is there showhide functionality for boiler plate content in google docs? - Stack Overflow
computed
is not triggered. Instead of manipulating properties of parts of your signals value, you should use theupdate
method of the signal. – Thomas Renger Commented yesterdaythis.dropdown.set(...this.dropdown())
. But without konwing the details i would recommend the version in my first comment. – Thomas Renger Commented yesterday