Leverage AngularJS to dynamically load CSS stylesheets using ng-repeat

I have organized my stylesheets into separate modules for each include, and I am trying to dynamically load them. However, I am facing some difficulties as when they are rendered, all I see is the following markup for each sheet that should be loaded:

<link ng-repeat="inlude in includes" rel="styesheet" href="" class="ng-scope">

Below is a snippet of my HTML code:

<!DOCTYPE html>
<html ng-app="forum" ng-controller="masterCtrl">
<head>
    <title>Forum</title>
    <link rel="stylesheet" href="/stylesheets/reset.css">
    <link rel="stylesheet" href="/stylesheets/style.css">
    <link ng-repeat="inlude in includes" rel="stylesheet" href="{{include.stylesheet}}">
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular-resource.min.js"></script>
    <script src="/scripts/app.js"></script>
    <script src="/scripts/controllers.js"></script>
    <!--[if lt IE 9]>
    <script src="/scripts/html5shiv.js"></script>
    <![endif]-->
</head>
<body ng-view>
</body>
</html>

Furthermore, here is an excerpt from my primary controller:

function masterCtrl($scope){
    $scope.includes = {
        header: {
            src: 'views/includes/header.html',
            stylesheet: 'stylesheets/header.css',
            searchQuery: ''
        },
        threadList: {
            src: 'views/includes/threadList.html',
            stylesheet: 'stylesheets/threadList.css'
        }
    };
}

All other functionalities seem to be working properly. Thank you for your assistance.

Answer №1

Make sure to adjust your href attribute to ng-href, and update the link structure as shown below to properly set the source for the stylesheets.

<link ng-repeat="include in includes" rel="stylesheet" ng-href="include.stylesheet" class="ng-scope">

By the way, the reason it wasn't functioning correctly was due to a typo in your code - you wrote inlude in includes.

Answer №2

It is advised to use ng-href instead of href attributes in your code. Please note that this method may impact the loading speed of your webpage.

Answer №3

For those interested, take a look at AngularCSS

This unique Angular module enables the inclusion of stylesheets within your components (directives) and pages (routes).

If you are utilizing directives

myApp.directive('miyDirective', function () {
  return {
    restrict: 'E',
    templateUrl: 'my-directive/my-directive.html',
    css: 'my-directive/my-directive.css'
  }
});

If you are utilizing ngRoute

$routeProvider
  .when('/my-page', {
    templateUrl: 'my-page/my-page.html',
    controller: 'pageCtrl',
    css: 'my-page/my-page.css'
  });

You can also utilize it as a service ($css)

myApp.controller('pageCtrl', function ($scope, $css) {

  // Attaches stylesheet(s) to scope create/destroy events (recommended over add/remove)
  $css.bind({ 
    href: 'my-page/my-page.css'
  }, $scope);

  // Simply include stylesheet(s)
  $css.add('my-page/my-page.css');

  // Simply remove stylesheet(s)
  $css.remove(['my-page/my-page.css','my-page/my-page2.css']);

  // Remove all stylesheets
  $css.removeAll();

});

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

Maintain hover effect of main menu on sub-menu in CSS3 flip dropdown menu

Check out the fiddle I created for my query https://jsfiddle.net/e7te8hf1/ <section id="action-bar"> <div id="logo"> <a href="#"><img src="img/logo.png"></a> </div><!-- end logo --> <nav class="navbar navigat ...

Ensure that the tab's content fills up the entire space provided and prevent the need for a vertical scrollbar to appear

Is there a way to make the tab content take up 100% of the available space without causing a vertical scroll due to the height of the tab itself? I am currently using ng-bootstrap and need assistance with this specific issue. You can review my code in the ...

angular ensuring seamless synchronization of objects across the application

This question pertains to both angular and javascript. In our angular app, we have numerous objects from the backend that need to remain synchronized. I am facing challenges in establishing efficient data bindings to ensure this synchronization throughout ...

Trouble with ASP.NET Master Page CSS Rendering Incorrectly

My CSS seems to be causing some trouble as the DIVs are not aligning as they should. Instead, they are just wrapping around the text and not adjusting the page layout properly. Here is an example of my CSS: body { height: 95%; width: 100%; ma ...

What is the best way to position text at the bottom of an image using CSS?

Utilizing the jQuery Caption Plugin to showcase the image alt text when hovering over an image. Currently, the text is displaying at the top but I would like it to show at the bottom. Any suggestions on how to remedy this? Desired Outcome: Check out the ...

Adjusting the size based on the screen width of various devices

I'm currently working on a website that is not responsive, and I know I can make it responsive using media queries. However, this will be a significant task... I'm looking for a quicker solution to simply zoom or scale my website to match the dev ...

Using Jade language in a standard web application without the need for Node.js

Can Jade be utilized in a traditional web application without reliance on Node.js? This question might seem unconventional considering Jade is built for Node, but I'm curious to know if its functionality can extend beyond the Node environment. ...

Can you explain the distinction between $translateChangeSuccess and $translateChangeStart and provide guidance on which one would be more suitable for my situation?

$rootScope.$on('$translateChangeSuccess', function () { A = $translate.instant('aaa'); B = $translate.instant('bbb'); C = $translate.instant('ccc'); }); Is there a difference between using $t ...

Tips for generating a .csv document using data from an array?

I am currently utilizing a PHP class to validate email addresses in real-time. The PHP Script is functioning as expected: validating the emails and displaying the results on the same page by generating a <td> element for each validated email. Howeve ...

The `innerHTML` function is responsible for closing HTML

My switch structure is set up as follows: switch (ratio) { case "16:9": imgContainer.innerHTML = '<img src="images/test-rata-de-aspect.jpg">' break case "4:3": imgContainer.innerHTML = '<img src="image ...

Using CSS selectors in Framework7 Vue allows for precise targeting and styling

I am currently working on developing a Cordova/Phonegap application using vue.js and the Framework7. I have been able to utilize functions like "onClick" by using the "v-on:click="OnClick" attribute within an HTML element. It's worth noting that Frame ...

Expanding your selection capabilities using BeautifulSoup

My objective is to harness the power of BeautifulSoup in a unique way. Within my HTML files, there are two specific tags that catch my interest. The first one, which I'll refer to as TagA, is <div class ="A">...</div> The second tag, k ...

Add fresh HTML elements within the ng-repeat loop

I'm struggling to insert new HTML code in the middle of an ng-repeat loop. I've tried using append but it doesn't seem to work. Here is a snippet of my code: <!DOCTYPE html> <html> <script src="https://ajax.googleapis.com/aja ...

Issue: Unable to redirect to 'login' page from 'app.home' state

I am in the process of converting a website into a mobile application, but I have encountered an issue. I am utilizing authO for third-party authentication, and when attempting to log in to the app, I received the following error: Error: Could not resol ...

Webix UI - Struggling to create dynamically responsive components during screen resizing

Can anyone guide me on how to make my Webix UI control responsive in the free version available at ? It seems to be acting unpredictably due to the AngularJS integration code I'm using. I have attempted various solutions like media queries, redraw, a ...

Can you guide me on how to duplicate a file when a button is clicked?

I am working on creating a one-page utility/prototype that has an admin user interface. The goal is to have a drop-down box for the admin user to select an option and then copy a file from one directory to another upon clicking a submit button. Although I ...

There is a discrepancy with the rectangular data object when trying to add a response interceptor, as it is undefined

After using Restangular, I encountered an issue where the get method/promise resolves, but the result passed to .then() is empty. A console.log(data) statement results in 'undefined'. Strangely, when checking the network tab in Chromium debug, th ...

Run the script within the Angular scope

Hey there! I'm having an issue passing a JavaScript code through Angular scope, but when it renders on the view page, it's displayed as text. I even attempted using ng-Sanitize, but unfortunately, that didn't solve the problem. &lt;div ...

Learn the process of invoking a function from Controller-B within Controller-A using AngularJS

Here is the code structure that I am currently working with: Controller.JS angular.module('RateRequestApp.controllers', []).controller('ReadOnlyController', function($scope, rateRequestService) { $scope.rateData = []; rate ...

Exploring Angular: Why is the link option important in directives?

As I dive into learning Angular, I often find myself struggling to grasp the code and its significance. Take a look at the example below and help me understand the purpose of the link option in directives. What exactly does the link option do? When does t ...