3

I am using UI-Router and want to change routing to be 'component based'. So Instead of defining a controller / template I want to use it like this:

.state('issue', {
    url: '/someUrl/:number',
    template: '<my-directive></my-directive>',
    resolve: {
      data: function(dataService) {
        return dataService.getData().then(function(response) {
            console.log(response.data);
            return response.data;
        });
      }
    }
 })

Now, I know that with Angular's ngRoute I can use resolved data directly in the template, for example:

 $routeProvider
      .when('/', {
        template: `<my-directive data="resolve.data"></my-directive>`,
        resolve: {
          data: function (dataService) {
            return dataService.getData();
          }
        }
      }) 

I couldn't do it using UI-Router (value was undefined).

Am I doing something wrong? Is it possible using ui-router?

3 Answers 3

4

The point with UI-Router is - result of resolve is available for a controller (related to template). So, we could do it like this:

.state('issue', {
    url: '/someUrl/:number',
    template: '<my-directive data="stateCtrlData"></my-directive>',
    controller: function($scope, data) { $scope.stateCtrlData = data },
    resolve: {
      data: function(dataService) {
        return dataService.getData().then(function(response) {
            console.log(response.data);
            return response.data;
        });
      }
    }
  })

The data are passed into controller and from its scope we pass it to directive.

Sign up to request clarification or add additional context in comments.

1 Comment

Great if that helped anyhow ;) Enjoy mighty UI-Router
0

If you mean injecting your data service, then you can do it like this (remember that the '' is telling to inject):

state('issue', {
        url: '/someUrl/',
        template: '<my-directive data="pep.data"></my-directive>',
        controller: function( data) { this.data = data },
        controllerAs: 'pep',  
        resolve:{
                 dataSvc : 'YourDataSvc',
                 campaign : function(dataSvc){
                         return dataSvc.getData();

                }
             }

Please remember a ui-view will be expected if you want to put additional views or child states.

Comments

0

Actually you can (tested only in ui-router v0.3.2)

There's an undocumented $resolve variable which is automatically injected in the controller. Simply add 'controllerAs' property to the state as follows, and you can use $resolve in the template:

.state('issue', {
    url: '/someUrl/:number',
    template: '<my-directive data="vm.$resolve.data"></my-directive>',
    controllerAs: 'vm',
    resolve: {
      data: function(dataService) {
        return dataService.getData().then(function(response) {
            console.log(response.data);
            return response.data;
        });
      }
    }
 })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.