简体   繁体   English

我如何在 Angular 中转义花括号

[英]How do I escape curly braces in Angular

I have this in my HTML:我的 HTML 中有这个:

<iframe src="https://kart.1881.no/?direction={59.83006424|6.25588723|START}{{{latitude}}|{{longitude}}|M%C3%85L}"></iframe>

Notice {{{latitude}} - Here I want to escape the first {注意{{{latitude}} - 这里我想逃避第一个 {

I tried to put \\{ but it not work as well.我试图把\\{但它不起作用。

The error is Missing expected : at the end of the expression错误是Missing expected : at the end of the expression

简单地尝试

{{'{' + latitude + '}'}}

You could try with %7B for { and %7D for } .您可以尝试将%7B用于{%7D用于} Those are just the URI-encoded forms, which Angular shouldn't try to parse.这些只是 URI 编码的形式,Angular 不应该尝试解析它们。

view看法

<iframe [src]="'https://kart.1881.no/?direction={59.83006424|6.25588723|START}{' + latitude + '|' + longitude + '|M%C3%85L}' | safe"></iframe>

And the next pipe should help you下一个管道应该可以帮助你

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer} from '@angular/platform-browser';

@Pipe({ name: 'safe' })
export class SafePipe implements PipeTransform {
    constructor(private sanitizer: DomSanitizer) {}
    transform(url) {
        return this.sanitizer.bypassSecurityTrustResourceUrl(url);
    }
}

You can override interpolation like你可以像这样覆盖插值

template: `
    <iframe src="https://kart.1881.no/?direction={59.83006424|6.25588723|START}{[[latitude]]|[[longitude]]|M%C3%85L}"></iframe>
  `,
interpolation: ['[[', ']]']

but anyway you have to sanitize url that will work only with property(not interpolation) binding但无论如何,您必须清理仅适用于属性(而非插值)绑定的url

Plunker ExamplePlunker 示例

Use the + operator to concatenate the required SRC使用+运算符连接所需的SRC

[src]="...{' + {{latitude}}+ '}...'"

(Update: added brackets to src ) (更新:在src添加了括号)

使用 html 实体&#x007B;

<iframe src="https://kart.1881.no/?direction={59.83006424|6.25588723|START}&#x007B;{{latitude}}|{{longitude}}|M%C3%85L}"></iframe>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM