Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ng-if in container component and checking property references #4934

Closed
baio opened this issue Oct 27, 2015 · 8 comments
Closed

ng-if in container component and checking property references #4934

baio opened this issue Oct 27, 2015 · 8 comments

Comments

@baio
Copy link

baio commented Oct 27, 2015

When I use container component with ng-ifwhich hide children elements (inserted via ng-content) it should not try to access properties bindings till ng-if is true

//Must use *ng-if=!!post to have not exception here
const template = `
    <modal [is-shown]="!!post">
        <modal-header><h2>{{post.title}}</h2></modal-header>
    </modal>

@Component({
    selector: 'post-modal',    
    template: template,
    directives: [Modal, NgIf]    
})
export class PostModal { 

    private post: any;              
}
const template = `
    <div class="modal" *ng-if="isShown">
        <div (click)="hide()">
            <ng-content select="modal-header"></ng-content>
        </div>
    </div>
`
@Component({
    selector: 'modal',    
    template: template,
   directives: [NgIf]    
})
export class Modal { 

    @Input() isShown: boolean

    constructor() {
        console.log("modal");
    }

}
EXCEPTION: TypeError: Cannot read property 'title' of undefined in [null]
@danturu
Copy link

danturu commented Oct 28, 2015

Why not just use an elvis operator? #791

@baio
Copy link
Author

baio commented Oct 28, 2015

If there is a lot of bindings you have to use ? for each. Anyway thinking this is not quite logically correct.

@danturu
Copy link

danturu commented Oct 28, 2015

On the one hand the result of the discussion I have mentioned above forced it to mark all the optional parts in the expressions: this makes things more obvious, but more laborious as well.

On the other hand this is a trivial situation when the property isn't predefined since a data usually comes asynchronously and we have to use the elvis operator in each case regardless of how many bindings there are.

So what's the difference with ng-if?

@baio
Copy link
Author

baio commented Oct 28, 2015

If ng-if is false child element shouldn't be rendered, hence binding shouldn't be activated. In this case (when child element is inserted via ng-content) this is not working correctly, binding still activating, when it shouldn't .

@danturu
Copy link

danturu commented Oct 28, 2015

Gotcha. Surely you are right. Here is a plunk that reproduces your issue - http://plnkr.co/edit/4xZOY0ujPE3tgl35aFwt?p=preview. The issue occurs because it renders the entire view before wrapping it in ng-if using ng-content.

@todoubaba
Copy link
Contributor

@baio ng-if could be set on modal element.

    <modal *ng-if="model">
      <modal-header>
        {{model.title}}
      </modal-header>
      <modal-content>
        {{model.content}}
      </modal-content>
    </modal>

see: http://plnkr.co/edit/C4HJMqrQfUeVwbEEHk31?p=preview

@vicb
Copy link
Contributor

vicb commented Jul 29, 2016

Please re-open if still an issue on the latest RC

@vicb vicb closed this as completed Jul 29, 2016
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants