I have a serious performance problem.
I want to show details in a list, but the function is called too many times.
You can try demo here
Here is HTML code :
<div ng-controller="MyCtrl as ctrl">
<p>Watch your console, then click on a letter. Why "myterious" function is launched so many time ???</p>
<button ng-click="ctrl.setValue('B')">Display B</button>
<button ng-click="ctrl.setValue('C')">Display C</button>
<button ng-click="ctrl.setValue('D')">Display D</button>
<div ng-repeat="item in ctrl.vitals track by $index">
<p ng-click="ctrl.toggleDetail($index)" ng-bind="item.name"></p>
<div ng-show="ctrl.myterious(item.name, ctrl.value)">This should only be display under {{item.name}}</div>
<div ng-show="ctrl.display[$index]">...</div>
<br>
</div>
</div>
And there is my JavaScript code :
var app = angular.module('myApp',[]);
app.controller('MyCtrl', [function() {
var self = this;
self.vitals = [
{name:'A'},
{name:'B'},
{name:'C'},
{name:'D'},
{name:'E'},
{name:'F'},
{name:'G'},
{name:'H'},
{name:'I'},
{name:'J'},
{name:'K'}
];
self.display = [];
self.toggleDetail = function(id) {
console.log("Toggle launched");
self.display[id] = !self.display[id];
};
self.value = 'B';
self.setValue = function (val) {
self.value = val;
}
self.myterious = function (a, b) {
console.log(a + ' ' + new Date().getTime());
if (a === b) {
return true;
} else {
return false;
}
}
}]);
I'd like to know where is the problem and how to fix it, because it is a simplification of my original code.