The code fails because it is waiting for view model which has quite a big size data (that's why I put $onInit but it seems not working). Once I debug it and after a certain period of time, the code runs smoothly, which means before assigning values to the variable, it's going through to check the length of the array. How can I make sure that data loaded first before everything else?
this.$onInit = function () {
$scope.viewModel = viewModel;
};
$scope.trueOrFalse= Array($scope.viewModel.length).fill(false);