0

i am using angular-ui-router and i have a resolve that brings me some data from the DB by a service.

now i want to inject it into my controller.

but, my controller is not defined in the state, rather just in my application.

this look like this:

 .state('myState', {
    url: "/create",
    templateUrl: "views/create.html",
    resolve: { 
        templates : function(templateService){
          return templateService.allMetadata.query({}, function(data){
          }, function(err){
            console.log(err);
          });
        }
    },
    controller: 'createCtrl'

so how can i inject my templates into my createCtrl which was defined elsewhere. trying to put a dependency injection my in createCtrl didn't work. it seems that the my only option is to decalre my controller inside the state decleration. this is obviously very bad.

any solution?

1
  • If templateService.allMetadata.query() is async, then make sure you return a promise from your resolve function. Then inject 'templates' into your controller. Commented Mar 4, 2014 at 19:00

1 Answer 1

1

One way or another you need to return a promise for an async operation. Assuming that templateService.allMetadata.query() is not returning a promise already, try something like this. Then inject 'templates' into your controller.

If you can modify it to return a promise, do that.

 .state('myState', {
    url: "/create",
    templateUrl: "views/create.html",
    resolve: { 
        templates : function(templateService, $q){
          var deferred = $q.defer();
          templateService.allMetadata.query({}, function(data){
              deferred.resolve(data);
          }, function(err){
            console.log(err);
            deferred.reject();
          });
          return deferred.promise;
        }
    },
    controller: 'createCtrl'
Sign up to request clarification or add additional context in comments.

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.