While creating a custom widget for visualizing dynamic table data, I encountered some issues and would like to share a minimal working example to illustrate the problem.
JS:
self.ctx.$scope.showAlert = function() {
window.alert("my device");
};
self.ctx.$scope.var = [
"id: 1, name: 'Batch A'",
"id: 2, name: 'Batch B'"
];
self.ctx.$scope.datasourceData = [];
self.ctx.detectChanges();
};
HTML:
<div class="mainCard">
<h3>Hello World</h3>
<button mat-raised-button color="primary" (click)="showAlert()">Click me</button>
<p>{{ctx.$scope.var}}</p>
<ul>
<li ng-repeat="item in ctx.$scope.var">
Wert: {{item}}
</li>
</ul>
<h3>Einzelne Werte</h3>
<p>Erster Wert: {{ctx.$scope.var}}</p>
</div>
What works:
- The alert button (showAlert) works correctly.
- Displaying the entire array ({{ctx.$scope.var}}) also works.
Problems:
- Accessing array items directly (e.g. {{ctx.$scope.var[1]}}) throws an error: "Widget Error: can't access property 1, ctx.ctx.$scope.var is undefined" -ng-repeat does not render any list items, although the array is initialized as expected in onInit.
Is there a recommended way to bind and access dynamic arrays within ThingsBoard widgets—especially when aiming to use ng-repeat or direct indexing?