Currently, I am working on a highly modularized project and focusing on building an Element Directive
that dynamically changes the templateUrl
based on user login/logout
.
To achieve this, I am attempting to execute a Factory's Function
within the templateUrl
. This specific function calls another method from a JWT Factory
and returns true
if the user is logged in or false
if not.
Based on the result received in the templateUrl
, I need to choose between different url
s for rendering purposes.
Unfortunately, I encountered the following error:
[$http:badreq] Http request configuration url must be a string. Received: {}
All $log.log()
statements display the correct results.
However, the problem persists as it fails to render either page1
or page2
.
Directive
(function () {
'use strict';
angular
.module('myApp')
.directive('myDirective', ['SessionCheckerFactory', function (SessionCheckerFactory) {
return {
restrict: 'E',
templateUrl : function(){
return SessionCheckerService.checkSession().then( function (res) {
console.log(res);//true
return res ? 'app/page1.html' : 'app/page2.html';
});
},
controller : 'MyController',
controllerAs : 'myCtrl',
bindToController : true
};
}]);
})();
SessionCheckerFactory
(function () {
'use strict';
angular
.module('myApp')
.factory('SessionCheckerFactory', function (AuthTokenFactory) {
function checkSession() {
return AuthTokenFactory.isAuth();
}
return {
checkSession: checkSession
}
});
})();
AuthTokenFactory
(function() {
'use strict';
angular.module('myApp')
.factory('AuthTokenFactory', function AuthTokenFactory(store, $cookies) {
//Takes user's info from LocalStorage, if not empty returns a String with encoded string informations
function getToken() {
if (store.get(key)) {
return store.get(key);
}
//Takes user's info from cookie
var token = $cookies.get('token', {path: '/'});
store.set(key, token);
return token;
}
//If getToken is empty returns false, else true
function isAuth() {
return Promise.resolve(Boolean(getToken()));
}
return {
isAuth : isAuth,
getToken : getToken
}
});
})();
I have researched and found that similar errors are typically related to $http
requests, but my case seems different. Unfortunately, I have not been able to find a solution yet.
Any suggestions on how to resolve this issue?
Thank you in advance.