1

I would llike tot test the performance of three frontend frameworks. AngularJS Angular 2 and EmberJS.

Currently I am trying to load 1000 objects from a number array to test the performance. I have encountered dificulties with loading the items from the array into a html list when loading the page.

app.component.ts:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
  <input type="button" value="load items">
 <ul>
 <li  (load)="ngOnInit() "*ngFor="let number of numberArray">
 <span class="badge">{{number}}</span> 
 <li>
 </ul>`
})




export class AppComponent implements OnInit {
  ngOnInit(): void {
    var numberArray: Array<number> = [];

    for (var i = 0; i < 1000; i++) {
      numberArray.push(i);
    }
    console.log(numberArray);
  }

};

To clarify my problem: I want to load the 1000 object from my array when I load the page I look forward to your answer

1
  • You will have to expose the array to the view Commented Jun 9, 2017 at 9:54

2 Answers 2

1

You have to bind numberArray to AppComponent class, so that it gets exposed to html.

So change var numberArray: Array<number> = []; to numberArray: Array<number> = []; ouside ngOnInit() function and then access via this

Do it like :

export class AppComponent implements OnInit {
numberArray: Array<number> = [];
  ngOnInit(): void {    
    for (var i = 0; i < 1000; i++) {
      this.numberArray.push(i);
    }
    console.log(numberArray);
  }    
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much
0

simply use your required html code inside a *ngIf as follows

<div *ngIf="numberArray.length > 0">
<input type="button" value="load items">
 <ul>
 <li  (load)="ngOnInit() "*ngFor="let number of numberArray">
 <span class="badge">{{number}}</span> 
 <li>
 </ul>
<div>

define numberArray variable as global variable

Comments

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.