2017-09-19 2 views
0

私のAPIがXML応答を返すという要件があります。 Angular2を使ってHTMLページに表示したいと思います。どのように達成するのですか? 私はXMLを解析するためにxml2jsノードモジュールを使用しています。 これは私がHTMLページで「こんにちはxml2js」を表示したい上記のコードから、私のコードAngular2のHTMLへのXML応答をバインドする

import * as xml2js from 'xml2js'; 
    /* This is sample demo I am working on.In future data will come 
     from API 
    */ 
    showData() { 
    const xml = '<root>Hello xml2js!</root>'; 
     xml2js.parseString(xml, function (err, result) { 
     console.dir(result); 
    }); 
    } 

です。 角度2でどのように達成できますか?

コンソールで私は以下の結果を得ます: enter image description here ありがとうございました。

答えて

0
import * as xml2js from 'xml2js'; 

    text = ""; 
/* This is sample demo I am working on.In future data will come 
     from API 
    */ 
    showData() { 
    const xml = '<root>Hello xml2js!</root>'; 
     xml2js.parseString(xml, function (err, result) { 
     this.text = result; 
    }); 
    } 
HTMLで

<p>{{text}}</p> 
+0

上記にしようとしましたが、エラーになっ "ERRORのTypeError例外を:未定義のプロパティ 'テキスト' に設定することはできません" コンポーネントで – hemantmali

+0

宣言文を。 text = ""; –