Angular array sanitization for handling multiple URLs

I need to sanitize multiple URLs from an array containing links to video sites e.g.:

  videos: SafeResourceUrl = ['www.someURL1', 'www.someURL2',...
  ];

To achieve this, I created a constructor like so:

  constructor(private sanitizer: DomSanitizer) {
    this.videos[this.trackByMethod(this.count)] = sanitizer.bypassSecurityTrustResourceUrl(this.videos[this.count]);
  }

In order to determine the position in the array, I implemented a method that retrieves the current index from the template:

  trackByMethod(index: number): number {
    return this.count = index;
  }

This is how my template is structured:

<div class="videos" align="center" *ngFor="let vid of videos; let i = index" [attr.data-index]="i" (ondrag)="trackByMethod(i)">
  <iframe [src] = "vid"  style='min-width: 30%; height: 315px' allowfullscreen>
  </iframe>
</div>

The issue I'm facing is that I am unable to retrieve the index from the template. The method trackByMethod with event binding

(ondrag)="trackByMethod(i)"
doesn't seem to work (I have tried various other interfaces of GlobalEventHandlers without success).

Could anyone offer assistance? Thank you!

Answer №1

Here is a suggestion:

      videoList: string[] = ['www.someURL1', 'www.someURL2',...
      ];
sanitizedVideos: SafeResourceUrl[];
    
    constructor(private sanitizer: DomSanitizer) {
this.sanitizedVideos =        this.videoList.map(video =>  sanitizer.bypassSecurityTrustResourceUrl(video);
      }

Implement the following in your HTML:

<div class="video-container" align="center" *ngFor="let vid of sanitizedVideos; let i = index" (ondrag)="trackByMethod(vid,i)">
  <iframe [src] = "vid"  style='min-width: 30%; height: 315px' allowfullscreen>
  </iframe>
</div>

Remember to utilize trackByMethod with both video and index parameters.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

a guide to transforming data into a string with json using angular

I am struggling to figure out how to bind my form data into a JSON string. My situation involves using a string field in the database and saving checkbox values in a single database column using JSON. I have created an HTML form, but I am unsure of how to ...

Guide to integrating Keycloak into Angular 6 applications

Looking for assistance on integrating Keycloak into Angular 6. Unsure of where to begin and how to initiate the Javascript Adapter. Can anyone lend a hand? ...

Guidelines for utilizing a loader to handle a TypeScript-based npm module

I am currently facing a challenge with my React and JavaScript project as I attempt to integrate an npm module developed with TypeScript. The issue lies in configuring my project to compile the TypeScript code from this module, resulting in the error messa ...

The CSS "mask" property is experiencing compatibility issues on Google Chrome

Hey there! I've recently developed a unique progress bar component in React that showcases a stunning gradient background. This effect is achieved by utilizing the CSS mask property. While everything runs smoothly on Firefox, I'm facing a slight ...

Turn off the touch events system for Ionic 2 on the leaflet map's draw controller

Seeking guidance on how to disable data-tap functionality in Ionic 2 within a leaflet map div. Anyone familiar with this? In Ionic-v1, the solution involved adding data-tap-disabled="true" to the map container (ion-content). I recently integrated the lea ...

I'm puzzled as to why I have to encode the path parameter twice in order for the REST call to properly handle special characters

I have a challenge with a REST call that fetches a product's ingredients using the product name as the path variable. I allow for any character to be used, and I know I need to use encodeURIComponent(name) to encode characters such as "/" to prevent m ...

Socket.emit allows for the transmission of various data points

Can someone help me with an issue I'm facing regarding socket.emit inside socket.on concatenating the same value after every emitting? Below is the code snippet on the server-side: io.on('connection', function(socket){ let balance = 6000; ...

Redux - a method of updating state through actions

Hello, I am currently working on developing a lottery system and I have a question regarding the best approach to modify state using action payloads. Let's consider the initial state: type initialCartState = { productsFromPreviousSession: Product[ ...

Transforming seconds into years, months, weeks, days, hours, minutes, and seconds

Can anyone help me modify Andris’ solution from this post: Convert seconds to days, hours, minutes and seconds to also include years, months, and weeks? I am currently running this code: getDateStrings() { console.log(req_creation_date); const toda ...

Having trouble injecting ActivatedRouteSnapshot into the component

Struggling to inject ActivatedRouteSnapshot into a component, encountering errors when trying to access query params. Here is the error stack trace: "Error: Can't resolve all parameters for ActivatedRouteSnapshot: (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?). a ...

Issue encountered when trying to bring in a component from a different module

I am attempting to import the 'OpenDialogContentComponent' component from Module A into Module B, however I am encountering this error: 'Cannot determine the module for class OpenDialogContentComponent in C:/Users/jitdagar/Desktop/TDP/pwt-u ...

Monitor the change in values upon pressing the submit button on Angular

I am currently working with an edit form that contains data in the input fields. <ng-form #infoForm="ngForm" novalidate> <div> <label for="firstName">First Name :</label> <input type=" ...

Using Angular Material to link a constant within an HTML file

I have set up my constants in the following way: constants.ts export const Constants = Object.freeze({ ACCEPTED_LEADS_TO_CALL: "Accepted Leads to Call", RECOMMENDED_CALL_LIST: "Recommended Call List" }); This makes it easy to refere ...

The type 'IProduct' cannot be assigned to type '[]'

Looking for help with dispatching events between parent and child components. Interfaces: export interface IProduct { id: string; name: string; price: string; image: string; inStock: number; fastDelivery: bo ...

Customizing the Style of Mat-Form-Field

I have designed a search bar using mat-form-field and attempted to personalize the appearance. Currently, there is a gray border-like region surrounding the input field and icons. Moreover, clicking on the input field results in a visible border: <form ...

Eliminate perfect-scrollbar functionality on mobile devices

I have applied the perfect-scrollbar class with 'height: auto' on mobile screens, expecting it to work properly. It's functioning well in responsive mode on the web. However, it does not scroll on mobile devices. @media (max-width: 992px) { ...

Enhance the capabilities of a basic object by incorporating a superclass through the creation of

I'm currently developing a library using Typescript 2.0 that can be utilized from both Typescript and JavaScript. Within the library, there is a class called Component and a function named registerComponent, both written in Typescript. My goal is to ...

Inferencing partial types in Typescript

I'm completely stuck on this and can't seem to figure it out without using a second function: interface Fixed { a: number } const fn = <A, B extends {} = {}>(b: B) => { return b } fn({ a: 1 }) // { a: number } fn<Fixed>({ a: 1 } ...

Angular module structure, is it appropriate to utilize shared components within the module?

I have been deliberating on the correct approach for designing my Angular project. The main entity in this application is an Order. An Order can be used in three different ways: Distribution Assigning Execution My decision was to create three modules: ...

Creating a Map in TypeScript from an Array

I have a series of TypeScript objects structured like this: interface MyObject { id: string, position: number } My goal is to transform this array into a map format where it shows the relationship between id and position, as needed for a future JSON ...