2016-10-10 16 views
6

का उपयोग करके आंतरिक एचटीएमएल के लिए सीएसएस प्रस्तुत करें मैं आंतरिक HTML का उपयोग करके एक HTML टेम्पलेट प्रस्तुत करने और SQL से प्राप्त एक HTML + css स्ट्रिंग प्रस्तुत करने का प्रयास कर रहा हूं।angular2

खाका स्ट्रिंग उदाहरण:

<html xmlns="http://www.w3.org/1999/xhtml"> <head><title>Template Name</title><style type="text/css"> p{ color:red; }</style> </head> <body> <h1>#headding#</h1> <p style="color:red;">#paragraph#</p><a href="#url#">#urltext#</a> </body> </html> 

अब यह एचटीएमएल ठीक से प्रस्तुत होती है जैसे कि यह शैली टैग चला जाता है और बस के अंदर पाठ renders यह लग रहा है।

enter image description here

एचटीएमएल प्रस्तुत करना हिस्सा: प्रस्तुत करना का

उदाहरण

<div [innerHtml]="templateBody"> 
</div> 

Home.component.ts भागों:

@Component({ 
    selector: "home", 
    templateUrl: `client/modules/home/home.component.html`, 
    encapsulation: ViewEncapsulation.Emulated 
}) 
export class HomeComponent implements OnInit{ 
    templateBody: string; 
.....other code 
} 

मैं कैप्सूलीकरण साथ यह कोशिश की है: ViewEncapsulation इम्युलेटेड/कोई नहीं आदि, इनलाइन सीएसएस की कोशिश की और मैंने मेजबान को जोड़ने की कोशिश की >>> पी टैग के सामने। वे सब एक ही प्रस्तुत करते हैं।

कोई सुझाव?

उत्तर

4

DomSanitizer साथ उसका उपयोग करें bypassSecurityTrustHtml और SafeHtml साथ,

डेमो: https://plnkr.co/edit/eBlzrIyAl0Il1snu6WJB?p=preview

import { DomSanitizer } from '@angular/platform-browser' 

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

@Component({ 
    selector: 'my-app', 
    template: ` 

     <div [innerHtml]="html | safeHtml"></div> 
    `, 
}) 
export class App { 
    name:string; 
    html: safeHtml; 
    constructor() { 
    this.name = 'Angular2' 
    this.html = `<html xmlns="http://www.w3.org/1999/xhtml"> <head><title>Template Name</title><style type="text/css"> p{ color:red; }</style> </head> <body> <h1>#headding#</h1> <p style="color:red;">#paragraph#</p><a href="#url#">#urltext#</a> </body> </html>`; 
    } 

} 
+0

धन्यवाद, इसकी पूरी तरह से काम कर रहे। –

+0

आपका स्वागत है @ShunGroenewald – micronyks

2

Sanitizer सम्मिलित करें और के रूप में https://angular.io/docs/ts/latest/api/platform-browser/index/DomSanitizer-class.html में प्रदर्शन Angular2 मालूम है कि आप सामग्री पर भरोसा करने के लिए HTML सामग्री से bypassSecurityTrustHtml(value: string) : SafeHtml लागू होते हैं।

भी देखें जिन्हें आप नीचे देख In RC.1 some styles can't be added using binding syntax

1

किसी भी पाइप के बिना मैंने किया और बस द्वारा मेरे घटक में DomSanitizer और SafeHtml इंजेक्शन और मेरे मार्कअप सेंट पर bypassSecurityTrustHtml चला रहा है अंगूठी। इसने मुझे अपनी इनलाइन शैलियों को पार्स आउट होने से रोकने की अनुमति दी।

import { Component, OnInit } from '@angular/core'; 
import { DomSanitizer, SafeHtml } from '@angular/platform-browser'; 

@Component({ 
    selector: "foo", 
    templateUrl: "./foo.component.html" 
}) 

export class FooComponent { 
    html: SafeHtml; 
    constructor(private sanitizer: DomSanitizer) { 
     this.html = this.sanitizer.bypassSecurityTrustHtml('<span style="color:##0077dd">this works</span>'); 
    } 
} 

और foo.component.html टेम्पलेट में

<div [innerHtml]="html"></div> 
+1

यह पोस्ट कम गुणवत्ता के रूप में चिह्नित किया गया था क्योंकि यह एक स्पष्टीकरण खो रहा था। अपने उत्तर पर विस्तार करने का प्रयास करें। –

+1

@ डेरेकब्राउन स्पष्टीकरण जोड़ा गया –