What does this line of code mean?

   $rootScope.show = function(text) {
      $rootScope.loading = $ionicLoading.show({
        content: **text ? text : 'Loading..',**
        animation: 'fade-in',
        showBackdrop: true,
        maxWidth: 200,
        showDelay: 0
      });
    };

May I know what does text ? text : ‘Loading…’, actually perform what action ?
Thanks.

content is declared as a function result. In this case it’s a “ternary if” by the looks of it.

That line literally reads

if(text != undefined){
return text;
}else{
return ‘Loading…’
}

Ternary operations tokenize as

condition ? true_val : else_val

As long as the “condition” (thing left of the ternary operator (?)) evals as true then true_val is returned, otherwise else_val is returned .

In Javascript anything that is not defined as false and anything which is not “undefined” will eval as true because (if x) means the same thing as (if x = x)

You don’t see that as much in Javascript, but I’ve seen it used a lot in other C descended languages.

I’ve never seen ** in JS except as part of a /** comment **/ and I didn’t try to run it, but that part doesn’t look like legal code to me. I’m guessing you added those characters to highlight the snippet of code? In C it would be pointer to a pointer. :smile: which threw me for a few minutes.

Hope that’s helpful!

3 Likes

Hey, thanks for your explanation.
Btw, I am sorry for the mistake to add the “*”. Haha.
It really confused people who know C language. LOLLLLL