UI-Router(解析)无法将数据传递到控制器

时间:2016-11-26 17:23:03

标签: javascript jquery angularjs angular-ui-router resolve

当我运行这样的代码时,我的控制器停止工作,它发生在从api或任何其他方式传递的所有类型的数据...我只是不能将任何东西传递给我的控制器

> $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();
> 
> 
> 
>                 }
>         }]);

只要我像这样运行它,它就可以完美地运行

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

即使我的州仍然像上面那样......

1 个答案:

答案 0 :(得分:0)

您可能有拼写错误或其他一些难以追踪的错误。 基于你的代码的JSfiddle运行得很好。

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>

输出

enter image description here