Working with signals in Angular 17, I encountered an issue while trying to update the value of a signal. The error message that I received is as follows:
NG0600: Writing to signals is not allowed in a `computed` or an `effect` by default. Use `allowSignalWrites` in the `CreateEffectOptions` to enable this inside effects.
Despite my efforts to find a solution, I have not come across any useful information.
Essentially, my goal is to update the signal value based on a specific condition within the Effect method:
Component:
export class LibGridViewComponent {
private _libSubjectsGiproService = inject(LibSubjectsGiproService)
constructor() {
effect(() => {
this.getValuesBySearchSignal()
});
}
getValuesBySearchSignal(displaySource?: any): any[] {
let testArr = [
{
"name": "B179 HEPATITIS VIRAL AGUDA NO ESPECIFICADA",
"value": "B179"
},
{
"name": "B980 HELICOBACTER PYLORI [H.PYLORI] COMO LA CAUSA DE ENFERMEDADES CLASIFICADAS EN OTROS CAPITULOS",
"value": "B980"
},
{
"name": "C799 TUMOR MALIGNO SECUNDARIO - SITIO NO ESPECIFICADO",
"value": "C799"
},
]
let searchTerm = this._libSubjectsGiproService.getSearchTerm()
let foundSource:any = []
if (searchTerm && searchTerm != '') {
foundSource = testArr.filter(i => i.name.toLowerCase().includes(searchTerm));
let currentSource = this.initialDisplaySource.value;
this.initialDisplaySource.next([...currentSource, ...foundSource]);
this._libSubjectsGiproService.setSearchTerm('')
} else {
this.initialDisplaySource.next([])
}
return this.initialDisplaySource.value
}
}
Service:
export class LibSubjectsGiproService {
searchTerm: WritableSignal<any> = signal<any>('');
setSearchTerm(value: any) {
this.searchTerm.set(value);
}
getSearchTerm() {
return this.searchTerm();
}
}
UPDATE: To provide more context on my objective, I have a grid view with a Select column that allows users to search for items. While it's possible to add new rows to the table, the search term remains the same. My aim is for the signal to be empty each time a new row is added, ensuring that initialDisplaySource is also empty until the user begins typing.