I'd like my app to generate the paths to my angular templates instead of hard-coding the string paths to them in my JS files. Currently I have the server creating a JSON object with all the info that I need. Here's how the rendered HTML appears:
<div ng-cloak ng-controller="BaseCtrl" ng-init="templatePaths = {
"items":[
{"token":"about","template":"http://localhost:32243/ViewTemplate/about.html"},
{"token":"contact","template":"http://localhost:32243/ViewTemplate/contact.html"},
{"token":"home","template":"http://localhost:32243/ViewTemplate/home.html"}
],"defaultTemplate":"http://localhost:32243/ViewTemplate/home.html"
}">
Previously I defined my routes like this, but as I would rather use the server-generated object above instead.
app.config([
"$routeProvider",
function ($routeProvider) {
$routeProvider
.when("/home", {
templateUrl: "ViewTemplate/home.html"
}).when("/contact", {
templateUrl: "ViewTemplate/contact.html"
}).when("/about", {
templateUrl: "ViewTemplate/about.html"
}).otherwise({
redirectTo: '/home'
});
}
]);
The problem I have is that since all my data about my routes is now on $scope.templatePaths
, I do not have access to $scope
from inside app.config
and I cannot find a way to add to the routes from inside the controller.
I did try this method, but it seems that it no longer works.
//Wait until templatePaths is init in the view...
$scope.$watch("templatePaths", () => {
_.each($scope.templatePaths.items, item => {
$route.routes[item.token] = { templateUrl: item.template }
});
});