Hey I am trying to pass some data in a form of json object from the controller
to a component , I using the annotation of '<' and listen to changes on field in the $onChanges method.
But apparently angular sense only one change, although I update the field (data field of current component) every 2 seconds using interval.
I have try to use the annotation of '=' but then I can't listen to changes on the object using $onChanges event.
So what can I do to solve this problem? Maybe I'm doing something which isn't right.
var app = angular.module("app",[]);
app.component('heroDetail', {
template: '<span>Name</span>',
controller: HeroDetailController,
bindings: {
data: '<'
}
});
function HeroDetailController(){
this.$onChanges = function(changes){
console.log("changes",changes);
}
}
app.controller('mainController',function($scope,$interval){
trendData = {
Load: [],
AVGTemperature: [],
IR: [],
Acoustic: []
}
$interval(function () {
for (var key in trendData) {
trendData[key] = [];
trendData[key].push((new Date()).getTime());
trendData[key].push(Math.round(Math.random() * 100));
}
console.log("$scope.trendData",$scope.trendData)
$scope.trendData = trendData;
}, 2000);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div ng-app="app" ng-controller="mainController">
<hero-detail data="trendData"></hero-detail>
</div>