Angular 4.x: How to load random.js into ngOnInit ()?

I'm new to angular 4.x I want to load change.js into the ngOnInit () function. How can I do this, I tried a little, but no one 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);
              });
          }
    ....
    ....
    ....
    }

You can help?

+4
source share
1 answer

finally found a solution.

In index.html

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

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 it out for free to improve this solution ...

+3
source

Source: https://habr.com/ru/post/1677658/


All Articles