2017-05-22 3 views
4

ang 4.xの新機能ngOnInit()関数でchange.jsをロードしたい。私はこれをどのように行うことができます私はいくつかのことを試してみましたが、どれも働いた:角4.x:ngOnInit()にchance.jsをロードするには?

ここに私のコードは次のとおりです。

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); 
       }); 
      } 
    .... 
    .... 
    .... 
    } 

あなたは助けてくださいことはできますか?

+1

あなたはjavascriptとangularのタグを付けましたが、あなたのコードはtypescriptのように見え、あなたのヘッダーはangularjsが角ではないと言いますか?非常に混乱し、おそらくあなたがこれを見て必要な人々を取得しません。これらはanglejとangleの間に大きな違いがあることに注意してください。 – Zze

答えて

3

が最終的に溶液を見出した。私のクラスで私のcomponent.ts

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

declare var Chance: any; // for externals librairies 

のindex.htmlで

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

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

このソリューションを改善するために自由に記入してください...

関連する問題