Kerim092 Kerim092 - 7 days ago 6
Javascript Question

UI-Router (resolve) cant pass data into controller

when i run code like this my controller stops working and it happens with all kinds of data passed with resolve, from api or any other way.. i just cant pass anything to my controller

> $urlRouterProvider.otherwise('/movies')
> $stateProvider
> .state('movies', {
> url: '/movies',
> templateUrl: 'templates/movies.html',
> controller: 'indexCtrl',
> resolve: {
> resolveParam: function(){
> return 'sdsadasdasd'
> }
> }
> })
>
> }]);
>
>
> app.controller('indexCtrl', ['$scope', 'resolveParam', function($scope, resolveParam) {
> $scope.click = function() {
> alert();
>
>
>
> }
> }]);


as long as i run it like this, it works perfectly fine

app.controller('indexCtrl', ['$scope', function($scope) {
$scope.click = function() {
alert();
}
}]);


even if my state still looks same like above..

Answer

You might have a typo, or some other hard-to-trace error. JSfiddle based on your code is working just fine.

http://jsfiddle.net/vcx171qa/

JS

angular.module('app', ['ui.router'])
  .config(['$urlRouterProvider', '$stateProvider', function($urlRouterProvider, $stateProvider) {
    $urlRouterProvider.otherwise('/movies')
    $stateProvider
      .state('movies', {
        url: '/movies',
        template: '<div>{{resolveParam}}</div>',
        controller: 'indexCtrl',
        resolve: {
          resolveParam: function() {
            return 'sdsadasdasd';
          }
        }
      })

  }])

.controller('indexCtrl', ['$scope', 'resolveParam', function indexCtrl($scope, resolveParam) {
  $scope.resolveParam = resolveParam;
}]);

HTML

<html>
   <head>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.9/angular.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.2/angular-ui-router.js"></script>
   </head>
   <body ng-app="app" class="ng-scope">
      <ui-view></ui-view>
   </body>
</html>

Output

enter image description here

Comments