Hi!
I’d like to inquire if there is any recommendation on how to access and manipulate data array from bucket in the AngularJS controller of a dashboard view.
I’ve been expanding from the original helloWidget example in documentation and trying to apply it to a HTML Timeseris widget. In this type of widget I find the data is made available via AngularJS expressions and can be accessed using numeric indexes. For example:
<p><strong>Timestamp</strong> is: {{value[0].ts}}</p>
<p><strong>Temperature</strong> is:... {{value[0].temperature}}</p>
<p>Value: {{value}}</p>
Will display the ‘ts’ and ‘temperature’ records in the ‘value’ bi-dimensional array.
But I need a way to access and consume this data in another js script. So I thought I could put a function in the controller to create a JSON in the HTML that I can use it in another separate script in the dashboard.
But so far all my attempts were not successful and result in ‘undefined’ error, even though the data is available in the $scope.value. This sequence will produce the following results:
console.log('Value: ', $scope.value);
console.log('Value[0]: ', $scope.value[0]);
console.log('Value[0].ts: ', $scope.value[0].ts);
Console:
I admit I’m pretty new to AngularJS and pretty much learning as I go. However I can’t find the reason why I’m unable to access de data rad from bucket from inside the controller even though the values appear to be there.
Any ideas that can point me in the right direction?
For context this is a screenshot of the dashboard, configuration and source code.
helloWidget.js:
angular.module('helloWidget', []).directive('helloWidget', function () {
return {
restrict: 'EA',
scope: {
source : "=",
ts: "=",
value: "="
},
templateUrl: function(){
let url = document.querySelector("script[src*='helloWidget.js']");
return url.src.replace('.js','.html');
},
controller: ["$scope", function($scope){
console.log("controller initialized! scope is", $scope);
// listeners for process source changes (if required)
$scope.$watch('source', function(newVal, oldVal) {
console.log("Source has changed:", newVal, oldVal);
});
// listeners for process value changes (if required)
$scope.$watch('value', function(newVal, oldVal) {
console.log("Value has changed:", newVal, oldVal);
});
console.log('THE SCOPE: ', $scope);
console.log('Value: ', $scope.value);
console.log('Value[0]: ', $scope.value[0]);
//console.log('Value[0].ts: ', $scope.value[0].ts);
let myValue = [];
myValue.push($scope.value);
console.log('MyValue: ', myValue[0]['temperature']);
console.log('Source: ', $scope.source);
$scope.sayHello = function () {
console.log("hello!");
}
}]
}
});
helloWidget.html:
<div>
<h3>Hello World from AngularJS directive!</h3>
<p><strong>Timestamp</strong> is: {{value[0].ts}}</p>
<p><strong>Temperature</strong> is:... {{value[0].temperature}}</p>
<p>Value: {{value}}</p>
<canvas id="myChart"></canvas>
<canvas id="myChart2"></canvas>
</div>
.
.
.
Thanks!