0

I'm trying to load specific CSS file using AngularJS controller.

HTML code looks like

<html lang="en" ng-app="CPCplusLunchLink" ng-controller="mainController">
<head>
    <meta charset="UTF-8">
    <script src="js/lib/angularjs/angular.min.js"></script>
    <script src="js/lib/angularjs/angular-route.min.js"></script>
    <script src="js/app.js"></script>
    <script src="js/controllers/main.js"></script>
    <script src="js/controllers/events.js"></script>
    <script src="js/services/events.js"></script>
    <link rel="stylesheet" ng-href="css/{{ css }}.css">

app.js related part

    .when('/', {
        templateUrl: 'views/home.html',
              controller: 'EventsController'
    })

mainContorller

myApp.controller('mainController', ['$scope', function($scope){


    // set the default css
    $scope.css = 'style';

}]);

and controller where I have set value for the CSS

myApp.controller('EventsController', ['$scope','events','$rootScope', function($scope, events, $rootScope){

// set dedicated CSS
$rootScope.css = 'style2';

    events.then(function(data) {
        $scope.eventsList = data.evets;

        console.log($scope.eventsList);
    });

}]);

but I'm not able to change default CSS value based on loaded controller.

Any clue what I have missed?

3
  • 1
    Maybe try using ng-src. But I dont think u can load css like this.Check out stackoverflow.com/questions/17695156/… Commented Aug 11, 2017 at 14:18
  • @Vivz I have tried to follow c-sharpcorner.com/blogs/… and scotch.io/tutorials/… Commented Aug 11, 2017 at 14:21
  • I just went through it and realized that you have to let angular know about the controller before you place the css Commented Aug 11, 2017 at 14:24

1 Answer 1

2

You have to let angular know about the dynamic CSS variable which is defined inside the controller. So you have to define the controller on HTML tag

<html lang="en" ng-app="myApp" ng-controller="EventsController">
Sign up to request clarification or add additional context in comments.

5 Comments

yep, example just for that here c-sharpcorner.com/blogs/…
@viz CSS I want to change using EventsController only for that particular view (home page only) and have it set to default for other views
Do you have a main controller apart from the partial ones? Try setting the variable on $rootScope or on $scope.$parent
@Viz No, unfortunately I don't. I will check that option and report back.
OK. I think I need to user $rootScope in both controllers and then do $rootScope.$apply(); in my EventsController to bind it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.