2017-01-26 19 views
0

私はデータを持つJSONファイルを持っています。各オブジェクトには私はこのようなフィールドにHTMLタグ、スタイル、およびテキストとの「説明」を持っている:angular 2 bypassSecurity

<div>__localname__</div> 
<div style="color: #555; margin-top:2px; margin-left:10px;"><div style="display:inline; color:#d34319 ">[DV]</div> Проверка домена (выдача от 5 минут)</div> 
<div style="color: #555; margin-top:2px; margin-left:10px;">Защищает домен с www и без www (указывайте при заказе домен с www, например www.domain.ru)</div> 
<div style="color: #555; margin-top:2px; margin-left:10px;">Гарантия 10000$</div> 

は私がホバー 問題でこのスタイルのデータを表示したいというタイトル([attrのです。これは私がデータを取得する方法である

@Pipe({ name: 'safeHtml'}) 
export class SafeHtmlPipe implements PipeTransform { 
    constructor(private sanitized: DomSanitizer) {} 
    transform(value: any) { 
    return this.sanitized.bypassSecurityTrustHtml(value); 
    } 
} 

:div要素のスタイルとブラウザが表示されないタイトル]または[タイトル]、または[タイトル])DomSanitizerと私はパイプを使用しています。このリンクhttp://g.co/ng/security#xss

を訪問する私を推奨しています:

01私はJSON

getPriceList() { 
    return this.httpGet('./ssl.json'); 
    } 

2)

ngOnInit() { 
    this.appService.getPriceList().subscribe(data => { 
     this.pricelist = data.pricelist; 
    }); 
    } 

3 Webページ上のそれが見えるように取得

1))私はホバー

<tbody> 
     <tr *ngFor="let item of pricelist"> 
     <td> 
      <a data-toggle="tooltip" data-html="true" 
      title="{{item.description|safeHtml}}"> 
       {{ item.name }} 
      </a> 
     </td> 
     <td> от {{ item.price.period[0].cost }} {{ item.price.currency }}</td> 
     <td> <div [innerHTML]='item.description | safeHtml'></div> </td> 
     </tr> 
    </tbody> 
にスタイルの "タイトル" を表示したいHTML、

[innerHTML]は問題なく動作しますが、タイトルは... JSONファイルからデータを操作するにはどうすればいいですか?ここで

はエラーとスクリーンショットです: https://yadi.sk/i/uieFFCfa3BMq4v

+0

ウェブサーバーでアプリを実行していますか?または単純なファイルベースのアプローチを使用していますか?これを見てください(http://stackoverflow.com/questions/3102819/disable-same-origin-policy-in-chrome) – Ken

+0

@Kenそれは問題ではありません、それは角2の消毒剤です、彼はメソッドを使用する必要があります'DomSanitizer'クラスの' bypassSecurityTrustHtml'です。 –

答えて

0

あなたのコンポーネントにこのPipeを追加します。

import { DomSanitizer } from '@angular/platform-browser'; 
@Pipe({ name: 'safeHtml'}) 
export class SafeHtmlPipe implements PipeTransform { 
    constructor(private sanitized: DomSanitizer) {} 
    transform(value) { 

    return this.sanitized.bypassSecurityTrustHtml(value); 
    } 
} 

ループ内のエレメントにPipeを使用できます。 例:

<div *ngFor="let price of pricelist"> 
    {{ price.description | safeHtml }} 
</div> 
+0

これはテーブルで動作し、タグ付きのすべてのデータが正しいことを示しています!ありがとうございました!ブートストラップツールチップと しかし、それはタグで作業をdoesntいくつかの理由で「titile」: ' {{ item.name }} ' そしてタグで、次のように表示さ https://yadi.sk/i/uieFFCfa3BMq4v –

+0

@GeorgeMadiarov使用してみてください'{{item.description | safeHtml}} '' ' –

+0

同じ問題: [attr.title] =" item.description | safeHtml " title =" {{attr.title} item.description | safeHtml}} " と多くの異なる音階 編集:メインポストを書き直す –