Use one angular signal store for multiple features (components)

I’ve defined angular signal store in parent table component and update data using

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>patchState
</code>
<code>patchState </code>
patchState
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export const TableStore = signalStore(
{ providedIn: 'root' },
withRouteParams({ id: param => Number(param) }),
withState(tableInitialState),
withMethods((store, tableApiService = inject(TableApiService)) => ({
getRows: rxMethod<number>(
pipe(
switchMap(structureId => {
patchState(store, { loading: true });
return tableApiService.getData(id).pipe(
tap({
next: (data: any) =>
patchState(store, {
rows: data.rows,
}),
error: () =>
patchState(store, {
rows: tableInitialState.rows,
loading: false,
}),
finalize: () => patchState(store, { loading: false }),
})
);
})
)
),
addRow: rxMethod<Row>(...)
),
})),
</code>
<code>export const TableStore = signalStore( { providedIn: 'root' }, withRouteParams({ id: param => Number(param) }), withState(tableInitialState), withMethods((store, tableApiService = inject(TableApiService)) => ({ getRows: rxMethod<number>( pipe( switchMap(structureId => { patchState(store, { loading: true }); return tableApiService.getData(id).pipe( tap({ next: (data: any) => patchState(store, { rows: data.rows, }), error: () => patchState(store, { rows: tableInitialState.rows, loading: false, }), finalize: () => patchState(store, { loading: false }), }) ); }) ) ), addRow: rxMethod<Row>(...) ), })), </code>
export const TableStore = signalStore(
  { providedIn: 'root' },
  withRouteParams({ id: param => Number(param) }),
  withState(tableInitialState),
  withMethods((store, tableApiService = inject(TableApiService)) => ({
    getRows: rxMethod<number>(
      pipe(
        switchMap(structureId => {
          patchState(store, { loading: true });
          return tableApiService.getData(id).pipe(
            tap({
              next: (data: any) =>
                patchState(store, {
                  rows: data.rows,
                }),
              error: () =>
                patchState(store, {
                  rows: tableInitialState.rows,
                  loading: false,
                }),
              finalize: () => patchState(store, { loading: false }),
            })
          );
        })
      )
    ),
    addRow: rxMethod<Row>(...)
    ),
  })),

Code above works as expected and rows can be accessed via

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>store.rows()
</code>
<code>store.rows() </code>
store.rows()

Within the table there is an edit action which opens dialog component for the clicked row. In the dialog component I’m trying to get those rows from the same signal store;

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export class LayerManagementDialogComponent {
tableStore = inject(TableStore);
ngOnInit() {
console.log(this.tableStore.rows()); <== EMPTY ARRAY??
}
}
</code>
<code>export class LayerManagementDialogComponent { tableStore = inject(TableStore); ngOnInit() { console.log(this.tableStore.rows()); <== EMPTY ARRAY?? } } </code>
export class LayerManagementDialogComponent {
   tableStore = inject(TableStore);

   ngOnInit() {
     console.log(this.tableStore.rows()); <== EMPTY ARRAY??
   }
}

but there are no data.

Does anyone know why? I’ve imagined table signal store to be domain level store and it’s state could be used by several features at the same domain.

I’ve tried to get data from the table store in dialog component and create another dialog store in order to inject table store and get data from there but rows are empty as well.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export const DialogStore = signalStore(
withMethods(
(
dialogStore,
tableStore = inject(TableStore),
tableApiService = inject(TableApiService)
) => ({
updateRow: rxMethod<Row>(
pipe(
switchMap(updatedRow => {
patchState(tableStore, { loading: true });
return tableApiService
.updateRow(updatedRow.id, updatedRow )
.pipe(
tapResponse({
next: () => {
console.log(tableStore.rows()), <= EMPTY ARRAY
patchState(tableStore, {
rows: tableStore
.rows()
.map(row =>
row.rowId === updatedRow.rowId
? { ...row }
: row
),
});
console.log(tableStore.rows()); <= EMPTY ARRAY
},
);
})
)
),
})
</code>
<code>export const DialogStore = signalStore( withMethods( ( dialogStore, tableStore = inject(TableStore), tableApiService = inject(TableApiService) ) => ({ updateRow: rxMethod<Row>( pipe( switchMap(updatedRow => { patchState(tableStore, { loading: true }); return tableApiService .updateRow(updatedRow.id, updatedRow ) .pipe( tapResponse({ next: () => { console.log(tableStore.rows()), <= EMPTY ARRAY patchState(tableStore, { rows: tableStore .rows() .map(row => row.rowId === updatedRow.rowId ? { ...row } : row ), }); console.log(tableStore.rows()); <= EMPTY ARRAY }, ); }) ) ), }) </code>
export const DialogStore = signalStore(
  withMethods(
    (
      dialogStore,
      tableStore = inject(TableStore),
      tableApiService = inject(TableApiService)
    ) => ({
     updateRow: rxMethod<Row>(
        pipe(
          switchMap(updatedRow => {
            patchState(tableStore, { loading: true });
            return tableApiService
              .updateRow(updatedRow.id, updatedRow )
              .pipe(
                tapResponse({
                  next: () => {
                    console.log(tableStore.rows()), <= EMPTY ARRAY
                      patchState(tableStore, {
                        rows: tableStore
                          .rows()
                          .map(row =>
                            row.rowId === updatedRow.rowId
                              ? { ...row }
                              : row
                          ),
                      });
                    console.log(tableStore.rows()); <= EMPTY ARRAY
                  },
              );
          })
        )
      ),
})

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật