Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to declare a specific type in ngFor

I am using ngFor to iterate a collection of a specific type [Menu] in Angular 4.x

Then looping on a collection property of the menu object [menu.items]

Unfortunately the property is unknown in my IDE [Eclipse + Angular IDE] even though the Menu class defines the items property as an array of MenuItem.

Any thoughts?

Red squiggles

Relevant class declarations -

export class MenuBase {
  id: string;
  title: string;
  isPublic: boolean;
  roles: string[];
  items: MenuItem[];
  position: number;
// rest of class omitted
}

export class MenuItem extends MenuBase {
  menuItemType: MenuItemType;
  callback: () => void;
  location: string;

  constructor (options: any) {
    super(options);
    this.location = options.location;
    this.menuItemType = options.menuItemType || MenuItemType.location;
    this.callback = options.callback;
  }
}

export class Menu extends MenuBase {
  constructor (options: any) {
    super(options);
  }
}

Additional info - This is the project I was working on: https://github.com/savantly-net/ngx-menu
The project will show an error in Eclipse, even though it's valid.

I never created any documentation, but I used it here -
https://github.com/savantly-net/sprout-platform/tree/master/web/sprout-web-ui

like image 349
Jeremy Avatar asked Sep 14 '17 17:09

Jeremy


People also ask

What does * mean in ngFor?

In *ngFor the * is a shorthand for using the new angular template syntax with a template tag, this is also called structural Directive.It is helpful to know that * is just a shorthand to explicitly defining the data bindings on a template tag. Follow this answer to receive notifications.

Can we call a function in ngFor?

Do it in code and assign it to each item and then just use it inside *ngFor . Such function calls from template bindings are discouraged.

How do you use ngFor in a tag?

We loop over each person in the people array and print out the persons name. The syntax is *ngFor="let <value> of <collection>" . <value> is a variable name of your choosing, <collection> is a property on your component which holds a collection, usually an array but anything that can be iterated over in a for-of loop.

Can I use ngIf and ngFor together?

Use ngFor and ngIf on same element It's very common scenario where we want to repeat a block of HTML using ngFor only when a particular condition is true. i.e., if ngIf is true.So in this case to use *ngIf and *ngFor on same element, place the *ngIf on a parent element that wraps the *ngFor element.


1 Answers

What I've had success with is creating a new component for the template that the *ngFor will render and have that typed.

Container template:

<ng-container *ngFor="item of items" >
  <my-custom-component [item]="item"></my-custom-component>
</ng-container>

Custom Component template:

<div *ngIf="item.menuItemType == 'dropdown'">
  <!-- -->
</div>

ts file:

@Component({})
export class MyCustomComponent {
  @Input() item: MenuItem
}
like image 166
C_Ogoo Avatar answered Oct 02 '22 16:10

C_Ogoo