I'm trying to have AngularJS' UI-Router pick up on the defined states (i.e. "project") and if none match, default to the "root" state. The "root" state should check with a remote API (Firebase) and based on the result load the appropriate view.
The example below doesn't accomplish either of those requirements:
1) "http://website.com/project" is matched to the "root" state, and not (the previously defined) "project" state.
2) "fbutil" is not defined in the "templateUrl" function, and cannot be injected.
Please help me resolve these issues.
angular.module('app')
.config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('project', {
url: '/project',
views: {
'mainView': {
controller: 'ProjectCtrl as project',
templateUrl: '/views/project.html'
}
}
})
.state('root', {
url: '/:id',
views: {
'mainView': {
templateUrl: function($stateParams, fbutil) {
var ref = fbutil.ref('user_data', id);
ref.once('value', function(dataSnapshot) {
return '/views/' + dataSnapshot.$val() +'.html';
}, function(error) {
return '/views/root.html';
});
}
}
}
})
}
]);
We cannot use templateUrl
here - as stated in the doc:
If templateUrl is a function, it will be called with the following parameters:
{array.} - state parameters extracted from the current $location.path() by applying the current state
The parameters coming to templateUrl are fixed.
So, we have to use templateProvider
function
Provider function that returns HTML content string.
templateProvider:
function(MyTemplateService, params) {
return MyTemplateService.getTemplate(params.pageId);
}
Check:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With