javascript - Loading dynamic list of iframes - "Error: Required a safe ResourceURL" - Stack Overflow

I am having trouble loading some iframe videos from YouTube, given a dynamic list of urls.I have this H

I am having trouble loading some iframe videos from YouTube, given a dynamic list of urls.

I have this HTML:

<div fxLayout="column">

  <div *ngFor="let v of videos">
    <div fxLayout="row">
      <div>
        Type: {{v.type}}
      </div>
      <div>
        <iframe width="420" height="315"
                [src]="getVideoURL(v)">
        </iframe>
      </div>
    </div>

  </div>

</div>

here is my Angular code:

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

@Component({
  templateUrl: './docsponent.html',
  styleUrls: ['./docsponent.scss']
})
export class DocsComponent implements OnInit {

  videos = [
    {type:'installation', val:''},
    {type:'usage', val: ''},
    {type:'keyboard shortcuts',val:''}
    ];

  constructor(private sanitizer: DomSanitizer) {

  }

  ngOnInit() {
  }

  getVideoURL(v: any){
    return this.sanitizer.bypassSecurityTrustUrl(v.val)
  }
}

I am getting this error:

Error: Required a safe ResourceURL, got a URL

Does anyone know how to fix this error?

I am having trouble loading some iframe videos from YouTube, given a dynamic list of urls.

I have this HTML:

<div fxLayout="column">

  <div *ngFor="let v of videos">
    <div fxLayout="row">
      <div>
        Type: {{v.type}}
      </div>
      <div>
        <iframe width="420" height="315"
                [src]="getVideoURL(v)">
        </iframe>
      </div>
    </div>

  </div>

</div>

here is my Angular code:

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

@Component({
  templateUrl: './docs.ponent.html',
  styleUrls: ['./docs.ponent.scss']
})
export class DocsComponent implements OnInit {

  videos = [
    {type:'installation', val:'https://www.youtube./embed/tgbNymZ7vqY'},
    {type:'usage', val: 'https://www.youtube./watch?v=ahla7JgpDEE'},
    {type:'keyboard shortcuts',val:'https://www.youtube./watch?v=bVYXWVs0Prc'}
    ];

  constructor(private sanitizer: DomSanitizer) {

  }

  ngOnInit() {
  }

  getVideoURL(v: any){
    return this.sanitizer.bypassSecurityTrustUrl(v.val)
  }
}

I am getting this error:

Error: Required a safe ResourceURL, got a URL

Does anyone know how to fix this error?

Share Improve this question asked Feb 6, 2018 at 2:09 Alexander MillsAlexander Mills 101k166 gold badges538 silver badges918 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Here's the answer:

use this:

 return this.sanitizer.bypassSecurityTrustResourceUrl(url);

instead of:

 return this.sanitizer.bypassSecurityTrustUrl(url);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745224844a4617411.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信