angularjs – 如何避免通过Angular-2链接添加前缀“unsafe”?
发布时间:2020-12-17 17:51:10 所属栏目:安全 来源:网络整理
导读:参见英文答案 In RC.1 some styles can’t be added using binding syntax????????????????????????????????????1个 我正在使用angular2和ionic 2开发移动应用程序, 我有3个按钮,用于拨打电话,短信和电子邮件,如下所示 a ion-button href="tel:{{contact.cel
参见英文答案 >
In RC.1 some styles can’t be added using binding syntax????????????????????????????????????1个
我正在使用angular2和ionic 2开发移动应用程序, 我有3个按钮,用于拨打电话,短信和电子邮件,如下所示 <a ion-button href="tel:{{contact.cellphone}}" color="primary"> <ion-icon name="call"></ion-icon> Call </a> <a ion-button href="sms:{{contact.cellphone}}" color="secondary"> <ion-icon name="text"></ion-icon> SMS </a> <a ion-button href="mailto:{{contact.email}}" color="dark"> <ion-icon name="mail"></ion-icon> Email </a> 电话和电子邮件工作正常,但不是短信,似乎角度添加不安全:在我的表达前面,它将变为如下 <a color="secondary" ion-button="" ng-reflect-color="secondary" class="button button-md button-default button-default-md button-md-secondary" ng-reflect-href="unsafe:sms:+16475374201" href="unsafe:sms:+16475374201"><span class="button-inner"> 即使我试过这个 <a ion-button [attr.href]="'sms:'+contact.cellphone" color="secondary"> <ion-icon name="text"></ion-icon> SMS </a> 还是一样的问题, 当我不使用角度绑定时,它工作正常,例如像这样 <a ion-button href="sms:+1647654654" color="secondary"> <ion-icon name="text"></ion-icon> SMS </a> 虽然它与angular有关,但我也检查了我的config.xml,并允许短信存在 <allow-intent href="tel:*" /> <allow-intent href="sms:*" /> <allow-intent href="mailto:*" /> <allow-intent href="geo:*" /> 更新 使用@Apuu解决方案后,它变得如下 <a ion-button [attr.href]="'sms:'+contact.cellphone | safeUrl" color="secondary"> <ion-icon name="text"></ion-icon> SMS </a> 解决方法
您可以创建管道服务以将不安全的URL更改为安全URL.
创建名为safe-url.pipe.ts的管道服务. import { Pipe,PipeTransform } from '@angular/core'; import { DomSanitizer } from '@angular/platform-browser'; @Pipe({ name: 'safeUrl' }) export class SafeUrlPipe implements PipeTransform { constructor(private domSanitizer: DomSanitizer) {} transform(url) { return this.domSanitizer.bypassSecurityTrustResourceUrl(url); } } 然后在您的视图中使用. <a ion-button [src]="'some_url' | safeUrl" color="secondary"> <ion-icon name="text"></ion-icon> SMS </a> 注意:不要忘记在app.module.ts中注入此管道服务 import { SafeUrlPipe } from './shared/safe-url.pipe'; //make sure your safe-url.pipe.ts file path is matching. 并在您的节点模块声明部分中. @NgModule({ declarations: [SafeUrlPipe],...}); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |