12

I would like to use proper dependency injection in MyCtrl1to inject the fields of the MyCtrl1.resolve object. I've tried many different combinations of attempting to inject @MyCtrl1.resolve etc. with no luck.

@MyCtrl1 = ($scope, $http, batman, title) ->
  $scope.batman = batman.data
  $scope.title = title.data

@MyCtrl1.resolve = {
 batman: ($http) ->
   $http.get('batman.json')
 title: ($http) ->
   $http.get('title.json')
}
#@MyCtrl1.$inject = ['$scope', '$http'] -- commented out because not sure how to inject resolve fields

 angular
.module( 'app', [])
.config( ['$routeProvider', '$locationProvider', ($routeProvider, $locationProvider)->
  $locationProvider.html5Mode(true)

  $routeProvider.when('/', {templateUrl: 'index.html', controller: MyCtrl1, resolve: MyCtrl1.resolve})
  $routeProvider.otherwise({redirectTo: '/'})
])

angular.bootstrap(document,['app'])
thorn0
  • 9,362
  • 3
  • 68
  • 96
jakecar
  • 586
  • 1
  • 7
  • 14

1 Answers1

22

Resolve is a property of a route and not a controller. Controllers would be injected with dependencies defined on a route level, there is no need to specify resolve properties on a controller.

Taking one of your examples (transformed to JavaScript), you would define your controller as always, that is:

MyCtrl1 = function($scope, $http, batman, title) {
  $scope.batman = batman.data;
  $scope.title = title.data;
}

and then the resolve property on a route:

angular.module('app', []).config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
  $locationProvider.html5Mode(true)

  $routeProvider.when('/',{templateUrl: 'index.html', controller: MyCtrl1, resolve: {
    batman: ['$http', function($http) {
      return $http.get(..).then(function(response){
         return response.data;
      });
    }],
    title: ['$http', function($http) {
      return //as above
    }]
  }});
  $routeProvider.otherwise({redirectTo: '/'});
}]);

If you want to minify the code using resolve section of routing you need to use array-style annotations - I've included this in the example above.

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
pkozlowski.opensource
  • 117,202
  • 60
  • 326
  • 286
  • Thanks for your response. However, I'm afraid I don't see how this helps me to inject 'batman' and 'title' into `MyCtrl1`. `MyCtrl1.$inject = ['$scope', '$http', 'batman', 'title']` would not work – jakecar Feb 07 '13 at 21:33
  • @jakecar it should work for globally defined controllers. For the controllers registered on a module (recommended) you would have to use array-style annotations. Let me know if you've got problems making it work, will prepare a plunk. – pkozlowski.opensource Feb 07 '13 at 21:40
  • 3
    I think you're missing your closing bracket `]` for `batman` and `title` – Aleck Landgraf Mar 12 '14 at 03:43
  • thank you for answering the main question - to use square bracket array syntax within the value section of the resolve object defintions. But would have been far better if your example code was actually syntactically correct - appears to be missing closing square brackets etc which was the crux of the answer. – arcseldon Apr 11 '14 at 17:13