I currently have an API that returns the url of a CSS file that I would like to have included into my whole site. Do to this url changing, I can not hard code the url. I currently have this working on 'view1' but what I want to do is have it work over the whole site. (so try to implement it in index.html). I am not sure what the best way to do this is, and I am sure my approach is wrong. Any insight would be helpful, or a solution to my issue.
My App is formatted as fallowed.
app/ --> all of the source files for the application
app.css --> default stylesheet
components/ --> all app specific modules
API/ --> Mock Slim API for testing
index.php --> Root for the API
view1/ --> the view1 view template and logic
view1.html --> the partial template
view1.js --> the controller logic
view1_test.js --> tests of the controller
view2/ --> the view2 view template and logic
view2.html --> the partial template
view2.js --> the controller logic
view2_test.js --> tests of the controller
app.js --> main application module
index.html --> app layout file (the main html template file of the app)
Here is View1 that is currently working
JS
'use strict';
angular.module('myApp.view1', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/view1', {
templateUrl: 'view1/view1.html',
controller: 'View1Ctrl'
});
}])
.controller('View1Ctrl', ['$scope', '$http', function($scope, $http) {
$http.get('/api/auth/test').
then(function(response) {
$scope.css = response.data.temp.css;
}, function(response) {
alert('Error retrieving css: ' + response);
});
}]);
HTML
<head>
<link ng-attr-href="{{css}}" rel="stylesheet" type="text/css">
</head>
Here is my root files that don't work
JS
'use strict';
// Declare app level module which depends on views, and components
angular.module('myApp', [
'ngRoute',
'myApp.authentication',
'myApp.view1',
'myApp.view2',
'myApp.version'
]).
config(['$routeProvider','$httpProvider','$locationProvider', function($routeProvider, $httpProvider, $locationProvider) {
$httpProvider.interceptors.push('TokenInterceptor');
// intercept API 401 responses and force authentication
$httpProvider.interceptors.push(function ($q, $location, AuthenticationService) {
//some code has been removed here
});
$routeProvider.otherwise({redirectTo: '/view1'});
}])
/*This is part of a test*/
.controller('', ['$scope', '$http', function($scope, $http) {
$http.get('/api/auth/test').
then(function(response) {
$scope.css = response.data.temp.css;
}, function(response) {
alert('Error retrieving css: ' + response);
});
}]);
/*This is part of a test*/
HTML
<head>
<link ng-attr-href="{{css}}" rel="stylesheet" type="text/css">
</head>
Question 1: Is there a better way of doing this? <-this is what I want
Question 2: If not, why is this not working? <- will settle for
My guesses are that the HTML is running before the JS and when the JS runs to change {{css}} into the correct response it's too late to include it. But If that were the case, why does it work on View1 and not the root index?
ng-controller? In your case you should consider binding it to<html>or<head><head>is out of scope of the controller. A directive would be better