arraysangularstring-interpolationangular-signals

Accessing signal array in the template


In the ServiceFile I have this signal:

answerSequence = signal<string[]>(['', '', '', '']);

I get this angular signal to my Component:

answerSequence = computed(() => this.questionService.answerSequence);

In my template I want to display the 4 positions of the array separately but the syntax does not work

<p>{{ answerSequence()?.[0] }}</p>
<p>{{ answerSequence()[0] }}</p>

I use Angular 17

can anyone help with this problem?


Solution

  • You need to invoke your signal in the computed to return its value :

    Component: answerSequence = computed(() => this.questionService.answerSequence()); 
    

    Which will allow you to write:

    {{ answerSequence()[0] }}