角度6-将字符串包含引号发送到组件输入

时间:2018-10-02 08:51:58

标签: angular

我有一个带有字符串输入的组件:

@Component({
   selector: 'abcComponent'
   template: `
      <div>
      ....
      </div>`
})
export class AbcComponent {
   @Input() text:string;
}

我要向组件发送包含引号的字符串(例如:abc“ d):

   selector: 'parentComponent'
   template: `
      <div>
         <abcComponent [text]="'abc"d'"></abcComponent>
      </div>`
})

我也尝试过:

<abcComponent [text]="'abc\"d'"></abcComponent>

但是在两种情况下,我都会收到模板解析错误。

有什么主意吗?

4 个答案:

答案 0 :(得分:1)

I found the way to do this is to sanitize your attribute value. Instead of using <abcComponent [text]="'abc"d'"></abcComponent>, use <abcComponent [text]="'abc&quot;d'"></abcComponent>, as &quot; is the "sanitized value" for the quotation marks.

Consider reading this answer on how to sanitize HTML into tokens to properly escape characters.

答案 1 :(得分:0)

我确定这可以解决问题

<abcComponent [text]='["abc\"d"]'></abcComponent>

答案 2 :(得分:0)

In component.ts

public text: string = 'abc\"d';

In component.html

<my-component [text]="text"></my-component>

答案 3 :(得分:0)

Try this

 <abcComponent [text]="'abc &quot; d'"></abcComponent>