Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4.x: How to load chance.js in ngOnInit()?

I am new in angular 4.x I want to load change.js in ngOnInit() function. How can I do this I have tried few thing but none worked:

here is my code :

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

...
.....
    declare var chance: any;

    import 'src/assets/js/chance.js';
    ....
    ....
    export class ComposantComponent implements OnInit {
    ....
    ....
          ngOnInit() {
            this.http.get("src/app/datas/data.json")
              .subscribe((data)=> {
                setTimeout(()=> {
                  this.data = data.json();
                  this.data.forEach(function(row, index) {
                    row.checked = false;
                     // use chance here ... 
                  });

                  console.log(this.data[0]);
                  // console.log(chance);

                }, 500);
              });
          }
    ....
    ....
    ....
    }

Can you please help ?

like image 459
theCode Avatar asked Oct 17 '22 12:10

theCode


1 Answers

finally found a solution.

In index.html

  <script src="http://chancejs.com/chance.min.js"></script>

in my component.ts

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

declare var Chance: any; // for externals librairies

In my class:

  chance: any;
  constructor(){
    this.chance  = new Chance();
    console.log(this.chance.ip());
  }

fill free to improve this solution ...

like image 184
theCode Avatar answered Oct 21 '22 01:10

theCode