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

Can't use scope variables inside a popover/tooltip TemplateReference #3490

Closed
javcode opened this issue May 24, 2019 · 5 comments
Closed

Can't use scope variables inside a popover/tooltip TemplateReference #3490

javcode opened this issue May 24, 2019 · 5 comments

Comments

@javcode
Copy link

javcode commented May 24, 2019

Reproduction link

https://stackblitz.com/edit/ng-zorro-antd-start-scmfqv

Steps to reproduce

Create a tooltip inside an *ngFor and try to access the declared variable from inside the ng-template

What is expected?

The scope variable should be accessible and rendered inside the ng-template used by the popover/tooltip

What is actually happening?

The variable is undefined

Environment Info
ng-zorro-antd 7.4.1
Browser Chrome
@wenqi73
Copy link
Member

wenqi73 commented May 24, 2019

It is not a problem of ng-zorro, angular does not support this:

<!-- Can not be rendered too -->
<div *ngFor="let item of items">
  <ng-container [ngTemplateOutlet]="popoverVarsContent"></ng-container>
</div>

<ng-template #popoverVarsContent>
  <p>Item: {{item | json}}</p>  
</ng-template>

You should create a new component and use @Input.

@wenqi73 wenqi73 closed this as completed May 24, 2019
@hsuanxyz
Copy link
Member

@javcode You can move ng-template to the loop body.

<div *ngFor="let item of items">
  <ng-container [ngTemplateOutlet]="popoverVarsContent"></ng-container>
  <ng-template #popoverVarsContent>
    <p>Item: {{item | json}}</p>  
  </ng-template>
</div>

@javcode
Copy link
Author

javcode commented May 24, 2019

I've ended up creating a new Component taking the 'item' as an input as I wanted to be able to re-use this code as it's used in several places.

export class ImportPopoverComponent implements OnInit {

  @Input()
  item:{}
}

Now the new component html looks like:

<a href="javascript:void(0)" nz-popover [nzContent]="popover">Add Item</a>

<ng-template #popover>
  <div>{{item}}</div>
</ng-template>

and I call it from the parent using:

<app-import-popover [item]="item"></app-import-popover>

Thanks wenqi73 for the work-around

@wenqi73
Copy link
Member

wenqi73 commented May 24, 2019

@javcode , @hsuanxyz 's work-around is more convenient.

@Areeb-Raza
Copy link

@javcode You can move ng-template to the loop body.

<div *ngFor="let item of items">
  <ng-container [ngTemplateOutlet]="popoverVarsContent"></ng-container>
  <ng-template #popoverVarsContent>
    <p>Item: {{item | json}}</p>  
  </ng-template>
</div>

Works like a charm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants