17

I have an outer component with a property:

Loading: Boolean = false;

this outer component is setting this property on a nested component:

<etp-loader text="loading..." loading="{{Loading}}"></etp-loader>

when I do typeof(this.Loading) on the nested component, I get string, which [what i believe] is what throws off the whole logic.

here's my nested component:

import { Component, AfterViewInit, OnInit, Input} from '@angular/core'
@Component({
    selector: 'etp-loader',
    template: `<div *ngIf="loading" class="progress" style="margin-left: 10%; margin-right: 10%;">
                <div class="progress-bar progress-bar-warning progress-bar-striped active" role="progressbar" aria-valuenow="100" aria-valuemin="0"
                        aria-valuemax="100" style="width:100%;">
                        {{text}}
                </div>
                </div>`
})
export class EtpLoaderComponent {
    @Input()
    text: string;

    @Input()
    loading: Boolean;    

    ngOnInit() {
        console.debug('init: loader state: ', 
                       this.loading, 
                       typeof(this.loading)); // => init: loader state:  false string
    }
}

edit - removed irrelevant bits

2
  • The typescript type boolean is all lowercase. Can you try it this way? Also, you are assigning the loading value wrong: Try: <etp-loader [loading]="Loading"> Commented Jan 25, 2017 at 16:40
  • bolean did't make a difference but [loading] was the issue :) Commented Jan 25, 2017 at 18:56

1 Answer 1

37

If Loading is a boolean value, then use

[loading]="Loading"

{{}} is string interpolation and the result will always be a string.

Angular doesn't know about boolean attributes. If you just want to know if a property is set or not, you can use a setter like

isLoading:Boolean;

@Input()
loading set(value: Boolean) {
  this.isLoading = value != 'false';
}

and use it like

<etp-loader loading>

and isLoading will be set to true

Sign up to request clarification or add additional context in comments.

4 Comments

thanks! very good to know about {{}} always string, makes perfect sense now.
BOO on angular. YAY for your answer!
@EricNovins no idea what the "BOO" is about, but thanks for the "YAY" :-)
May be the "BOO" was like me I specified ShowIndex="false" in the HTML template, ie, passing a literal FALSE, and I was getting a string "false", changed it to [ShowIndex]="false" even though I don't have a variable named "false" nor do I intend to bind this param necessarily :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.