0

I have a getter to return a value that is calculated in another function. It has the form like this:

private get numErrors() {
    return calculateErrors(key);
}

In my html I have {{ $ctrl.numErrors }}. I am using AngularJS. This renders {{ $ctrl. numErrors }} for a few seconds before the value numerical value is calculated and displayed. Is there a way I can get it to initially display 0 before I get a return value?

mysticalstick
  • 2,479
  • 5
  • 16
  • 21
  • 1
    See if it can help you http://stackoverflow.com/questions/12866447/prevent-double-curly-brace-notation-from-displaying-momentarily-before-angular-j – Diullei Apr 03 '17 at 18:27

2 Answers2

0

You could potentially get around this via ng-cloak.

But instead of having numErrors() as a getter directly returning your other function's results why not do something like this? Have calculateErrors() get called in your logic where you need it to and have it set the value. This will be automatically updated on your template.

numErrors: number = 0;

calculateErrors(key) {
    // Do calculation
    numErrors = resultOfCalculation;
}
Ben
  • 2,441
  • 1
  • 12
  • 16
-1

Considering that it is numeric value and it shouldn't be checked if it strictly equals to undefined, it can be

private get numErrors() {
    return calculateErrors(key) || 0;
}

Or

{{ $ctrl.numErrors || 0 }}
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • this does not work because the function doesn't return an error it just takes a while to process – mysticalstick Apr 03 '17 at 21:01
  • The question and the answer - both of them - don't t say anything about errors. What is 'a while to process' supposed to mean? If it doesn't return a value immediately (i.e. the value is calculated and be available on the next digest), `0` is returned instead. The code in the question can't explain why it wouldn't work. – Estus Flask Apr 03 '17 at 21:15