How to implement the service in the function of the AngularJS UL router template?

I am trying to configure the AngularJS UL router to certain states (ie, "project"), and if they do not match, the default is "root". The root status should be checked using the remote API (Firebase) and, based on the result, load the corresponding view.

In the example below, none of these requirements are met:

1) " http://website.com/project " corresponds to the state of "root", and not to the (previously defined) "project".

2) "fbutil" is not defined in the function "templateUrl" and cannot be entered.

Please help me solve these problems.

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'; }); } } } }) } ]); 
+5
source share
1 answer

We cannot use templateUrl here - as stated in the doc :

templateUrl (optional)

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 have been fixed.

So we have to use templateProvider

templateProvider (optional)

function

A provider function that returns a string of HTML content.

 templateProvider: function(MyTemplateService, params) { return MyTemplateService.getTemplate(params.pageId); } 

Check:

+4
source

Source: https://habr.com/ru/post/1236482/


All Articles