Utilize dynamic CSS application within an Angular application

In my Angular application, I dynamically load URLs inside an iframe and want to apply custom.css to the loaded URL once it's inside the iframe. I attempted using ng-init with angular-css-injector function in the iframe, but the CSS is not being applied, likely due to it being called at an early stage. Is there an Angular feature to execute the function once the URL is loaded in the iframe?

Here is the code HTML:

<iframe frameborder='0' ng-src="{{trustSrc(selected.imgurl)}}" ng-init="applyCSS()"   frameborder="0" allowfullscreen>
</iframe>

App.js:

var routerApp = angular.module('DiginRt', ['ngMaterial','angular.css.injector'])

routerApp.controller('AppCtrl', ['$scope', '$mdSidenav','$sce', 'muppetService', '$timeout', '$log', 'cssInjector',function($scope, $mdSidenav, $sce, muppetService, $timeout, $log, cssInjector) {

        $scope.applyCSS = function() {
            cssInjector.add("style.css");
            console.log("loaded css dynamically");

        } 

Here is the plunker

Answer №1

Attempting to append CSS to your own page will not affect the child document created by an iframe. This is because the iframe generates a separate child document once the src has been loaded into the DOM.

Unfortunately, it is not possible to apply styles from the parent document to the iframe's child document in this manner. However, you can dynamically add styles inside the iframe as long as both domains are the same. (Check out this SO Answers for more information on loading content from the same domain)

Based on the code provided, it seems like you are not attempting to load content from a different domain within the iframe on your website.

A potential solution could involve passing an additional query parameter in the URL and managing the CSS code on the site you want to load inside the iframe based on that query parameter. You can use yepnope.js to load JavaScript conditionally.

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

Issues arise when Angular Meteor fails to load the UI-Router properly

Currently, I am exploring the integration of ui-router for routing within a meteor-angular application. My reference point is the meteor Whatsapp tutorial which can be found here Below is the relevant code snippet related to ui-router implementation: log ...

JavaScript code for opening and closing buttons

I created a button that successfully opens, but I am struggling to figure out how to close it. Can someone assist me with this? Additionally, I have one more query: How can I remove the border when the button is clicked? document.getElementById("arrowb ...

What is the reason for sending a JSON string in an HTTP POST request instead of a JavaScript object in AngularJS

When sending data via a post request in AngularJS, the code may look like this: $http({ method: 'POST', url: url, data: inputParams, headers: headers }) .then(function succes(response) { successCallBack(response.data); } Here is h ...

Navigating Parse object attributes within AngularJS

Currently, I am in the process of developing an AngularJS application that utilizes data from a parse-server backend. To retrieve this data, I use services that are called from my controllers. However, as Parse objects require all properties to be accessed ...

When the down key is pressed in a textarea, choose the list item

I have HTML similar to the following <div class="row"> <textarea id="txtArea" ng-model="input" ng-change="handleOnChange(input);getSearchField(input);" ng-click="search(input)" ng-focus="search(input);" ...

Transitioning React Hover Navbar Design

I'm currently revamping a click-to-open navbar into a hover bar for a new project. I have successfully implemented the onMouseEnter and onMouseLeave functions, allowing the navbar to open and close on mouse hover. However, I am facing an issue with ad ...

Using the calc function to define input width may not yield the expected results

I am facing an issue where I have an input element with width: calc(100% - 100px); and no padding/margin/border. Next to this input, I want to place a div with position: inline-block; and width: 100px;. The problem is that the div is going to the next lin ...

Loading images with CSS media queries is an essential technique for optimizing

Currently, I am exploring options to dynamically load a background image based on the width of the browser window. This way, I can optimize bandwidth usage by only loading the image that will be displayed. I am aware that using the HTML picture tag allows ...

Troubleshooting problems with dynamic imported asset URLs in Vue and Vitest snapshot snapshots

After migrating my project from Vue-CLI & Jest to Vite & Vitest, I encountered a problem when trying to run tests through Jenkins. It seems that some snapshot images are failing to match due to discrepancies in asset paths. Expected : src="file:///C: ...

There was an issue with the Google Maps embed API that resulted in an error with interpolation

My goal is to utilize the Google Maps API in order to showcase a map using data from a JSON file. However, whenever I attempt to incorporate the JSON data, an error message 'Error: [$interpolate:noconcat] Error while interpolating' pops up. < ...

I'm looking to divide the background horizontally into two sections, with one side being a solid black color and the other side incorporating a gradient of two colors, such as purple transitioning into pink

body { height:100%; background: linear-gradient(top, #d808a4 50%, black 50%); background: linear-gradient(top, #d808a4 50%,black 50%); background: linear-gradient(to bottom, #d808a4 50%,black 50%); height: 229vh; } I am trying to creat ...

I noticed an excess of white space on the right side of my Angular website

Check out my website here. Everything seems to be functioning correctly, however, there is an issue with scrolling on mobile. It should only scroll up and down, not left and right! I have noticed a strange white space on the right side of my site when view ...

Is it time to go back to the parent selector using the ampersand symbol?

I recently started using Less for writing CSS, and it has definitely been a time-saver for me. However, I have encountered a small issue with my code: <a href="#" class="btn-black-bg btn-right-skew">largest fight gym in Australia</a> Less .b ...

What is the best way to center my text vertically within a Bootstrap 5 "col" class division?

Struggling to create a Bootstrap 5 page and facing challenges with vertically aligning text in divs? Here's the code snippet causing problems: <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" ...

Plunker fails to run simple AngularJS demo

I'm having trouble figuring out why this basic example isn't functioning as expected on Plunker. http://plnkr.co/edit/EfNxzzQhAb8xAcFZGKm3?p=preview var app = angular.module("App",[]); var Controller = function($scope){ $scope.message ="Hel ...

Both the maxlenght and ng-maxlength directives appear to be ineffective in AngularJS

In my HTML file, I have the following input: <input name="password" id="newPasswordConfirmation" ng-model="newPasswordConfirmation" type="number" inputmode="numeric" placeholder="" required ...

The table contained within a row-spanning cell fails to fully occupy the cell's height across different browsers

Chrome rendering and code issue I'm struggling to understand why the nested table (highlighted in red) is not taking up the full height of the outer table cell (highlighted in green) which spans 8 rows. This used to work fine, but newer browsers are ...

Tips for Positioning Sidebar Elements Relative to Content IDs

My goal is to have sidebar elements align with specific id tags in the main content, rather than just stacking up one after another. I want the sidebar elements to align with certain id tags mentioned in the main post content. The image at shows the basic ...

Hide an Angular button when a page is loaded, depending on the information found in a table

How can I hide the Submit button in a table row if a certain condition is met based on information from the database? I attempted to create a function that returns true or false, but it ends up crashing my program because it runs continuously. I also trie ...

Is there a way to have AngularJS display the date without factoring in time zones?

When retrieving a date from WEBapi, it returns in the format: 2013-01-01T00:00:00 I need it to display as: {{msa.StartDate | date:'yyyy-MM'}} Currently, it shows as: 2012-12 Is there a way to make it ignore time zones and display as: 2013- ...