Using $sce in ng-repeat within Angular configuration parameters

I have been trying to incorporate videos using iframes, but no content is being displayed even with the correct embed link. Strangely, when I manually input the link into the iframe tag, the videos show up as expected. Here is the code snippet where I am facing the issue:

<ion-item ng-repeat="article in articles" class="item-light">
    <img ng-show="article.external_media.length == 0 || article.external_media.url == ''" src="http://coop.app/imagecache/cover/{{article.cover_image}}">
    <iframe ng-show="article.external_media.length > 0 && article.external_media.url != ''" src="{{article.external_media[0].url}}"></iframe>
</ion-item>

Update

Considering the need to include $sce dependency, I am unsure of how to implement it for all potential links within my controller. What would be the correct approach for this function?

Below is my controller setup:

.controller('FrontPageController', function($scope, ArticleService, $state) {
  ArticleService.all().then(function(data){
    $scope.articles = data;
})

Answer №1

Is there a way to use the $sce dependency to handle all links in my controller? What would be the best approach to achieve this?

One solution could be creating a filter for this purpose.

.filter( 'trustedUrl', [
    '$sce'
    function( $sce ){
        return function(url){
             //implement necessary logic here
             return $sce.trustAsUrl(url)
        }
    }
])

In your HTML:

<iframe src="{{article.external_media[0].url | trustedUrl}}">

I prefer using filters over adding methods to controllers as it helps in keeping the controllers lightweight. Filters are especially useful when content needs to be processed.

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

Utilize XML and Javascript to create a captivating donut chart

I am currently experimenting with XML and JavaScript to generate a donut chart. I attempted to implement this JS code. However, I am struggling to create XML that is compatible with the JS. var SCIENCE = 0; var ART = 0; var MATH = 0; $(document).ready ...

How can I use jQuery to add styling to my menu options?

Looking to customize my menu items: <ul> <li class="static"> <a class="static menu-item" href="/mySites/AboutUs">About Us</a> </li> <li class="static"> <a class="static-menu-item" href="/m ...

What is the best way to pass the ng-repeat value to the controller in AngularJS

On my webpage, I am trying to utilize ng-repeat with an array like the one below: var array = [{name: Bill, age: 12, number: 1}, {name: Tyrone, age: 11, number: 2}, {name: Sarah, age: 14, number: 3}]; I want to have a button that, when clicked, sends eit ...

Converting Rails application from AngularJS to ERB

I am currently working on integrating AngularJS into my code <button class="btn" ng-click="editUser(user.id)"> <span class="glyphicon glyphicon-pencil"></span>  Edit </button> I need to convert the foll ...

Issue with sending data through $http PUT

I am currently in the process of developing a basic auction platform. After displaying my products on the website, I now want to introduce a new feature - making offers. In order to implement this functionality, I have used ng-dialog in my controller to op ...

When JSON contains slashes, JSON.parse will trigger an error

I am struggling with a valid JSON generated using PHP like this: var json = <?php echo json_encode(['somearray']); ?>. Inside the array, there is an HTML string as shown below: $myArray = ['image' => '<img src="/img/f ...

Using Angular JS to apply filters conditionally

Average Product and Channel Price: {{average_price | currency}} The average price is represented as a string with an initial value of 'None', which cannot be 0.0. Initially, the value 'None' is not displayed when applying the currency ...

How can you integrate external JavaScript libraries into your project using Bower?

After downloading a javascript library, I added it to my project by pasting it into index.html. But whenever I run a Bower serve or build, the library gets overwritten. For my other javascript libraries, I could easily add them by using: bower install -- ...

After the recent update to version 4.2.0 of @material-ui/core, unexpected react hook errors have been

Currently, I am working on an electron application that utilizes react and material-ui. Recently, I made the decision to update material-ui to version 4.2.0. As a result of this update, two lines were added to my dependencies in the package.json. "@materi ...

creating a form using AngularJS within a Bootstrap modal window

I'm currently facing an issue with a form that is located inside a Bootstrap modal. I am unable to retrieve the form values on the controller side using scope variables. If you want to take a look at the code, here is the link: https://plnkr.co/edit/ ...

Guide on keeping your MongoDB collection up-to-date with Mongoose

I'm currently facing an issue while working on a project. My goal is to build a MongoDB database where I can store dates and YouTube video links. I've chosen to use Mongoose as my ORM. The problem I'm encountering is that while I can create ...

Jump to Anchor when Page Reloads

Hey there! I'm currently developing a gallery script and I'm trying to figure out how to automatically position the page to the top of a specific anchor when it is loaded. I attempted to use the following code: <script>location.href = "#tr ...

Determine whether all elements in the array are false using Array.every()

Below is an example of an array: myArray = {firstValue: false, secondValue: false, thirdValue: true, forthValue: false}; The goal is to determine if every value in the array is false. If that condition is met, then perform a specific action. For instance ...

Troubleshooting undefined return value in function when passing select list value in AngularJS

I am working with 3 select lists and attempting to transfer the selected value to a function. $scope.mapSelections = function(location) { var filteredPins = []; console.log('location:' + location); var region = (lo ...

Obtain asynchronous view model for component

Is it possible to retrieve a view model from the server and incorporate it into my component? I attempted to do so with the following code snippet, but it is not working as expected: function fetchViewModelFromServerAsync(){ setTimeout(function(){ ...

The configuration error occurred for the `get` action due to an unexpected response. Instead of an object, an array was received

Despite numerous attempts, I am struggling to find a solution that works for me. In my Courses controller, I am using the Students service and Staff service to access my staff and student objects. My goal is to retrieve the staffs and students objects in o ...

What is the best way to store a username and password within a JavaScript object in ReactJS?

I am encountering an issue with obtaining the login credentials from the object. Although the code snippet below functions perfectly well with other forms. SignIn.js export default function SignIn(props) { const [state, setState] = useState({ userna ...

Enhance your checkbox and radio components with React Higher Order Components (H

I'm in the process of designing my own custom checkbox and radio components to ensure reusability. This is what I have so far: import React, { Component } from 'react' export class Checkbox extends Component { render() { return ...

Combining an HTML file, a D3.js script file, and manually inputting data in the HTML file

I am relatively new to d3.js and currently working on building a simple application. However, I have encountered a roadblock in my progress. I have a separate JavaScript file named jack.js which generates a pie chart when linked with an HTML page. The Iss ...

I am having difficulty in receiving accurate speech recognition through the microphone while using the Azure Speech to Text service in a

I developed a code in React JS to extract text from speech using the microphone, but unfortunately it is not able to recognize or identify the speech. I have tried various options but all efforts seem to be in vain as no errors are being shown. const start ...