【问题标题】:Error: [ng:areq] Argument 'searchCtrl' is not a function, got undefined错误:[ng:areq] 参数 'searchCtrl' 不是函数,未定义
【发布时间】:2015-11-03 14:20:12
【问题描述】:

我正在尝试让 ui-router 工作,而且我已经非常接近了。好吧,我让它工作了,但我无法将它与我的控制器结合起来,然后我得到了错误。

我在控制台中收到此错误

Error: [ng:areq] Argument 'searchCtrl' is not a function, got undefined

searchCtrl.js

var myApp = angular.module('myApp')

  .controller('searchCtrl', [

    '$scope', '$http', function($scope, $http) {

      $scope.search = function() {

        var base = 'http://api.themoviedb.org/3';
        var service = '/search/movie';
        var apiKey = 'a8f7039633f2065942cd8a28d7cadad4&query='
        var search = $scope.searchquery
        var callback = 'JSON_CALLBACK'; // provided by angular.js
        var url = base + service + '?api_key=' + apiKey + search + '&callback=' + callback;

        $http.jsonp(url,{ cache: true}).
          success(function (data, status, headers, config) {

            if (status == 200) {
              $scope.movieList = data.results;
              console.log($scope.movieList)
            } else {
              console.error('Error happened while getting the movie list.')
            }

          }).
          error(function (data, status, headers, config) {
            console.error('Error happened while getting the movie list.')
          });
      }

    }
]);

我的 searchModule.js

var app = angular.module('movieseat', ['ui.router']).config([

  '$urlRouterProvider', '$stateProvider', function($urlRouterProvider, $stateProvider) {
    $urlRouterProvider.otherwise('/state1');
    return $stateProvider.state('state1', {
      url: '/state1',
      templateUrl: 'assets/angular-app/templates/state1.html'
    });
  }

]);

还有我的 app.js.coffee

@app    = angular.module('movieseat', []);
@myApp  = angular.module('myApp',[]);

# for compatibility with Rails CSRF protection

@app.config([
  '$httpProvider', ($httpProvider)->
    $httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content')
])

@app.run(->
  console.log 'angular app running'
)

This might be the same question 并且我尝试将另一个模块添加到我的 app.js.coffee 但它不起作用。

有人给点小费吗?

【问题讨论】:

  • 你想在这里创建两个不同的模块吗?如果答案是肯定的,那么将 myApp 模块注入到 movieseat 中。您在此处与模板绑定哪个模块?
  • @SatyamKoyani 我不认为我想要 2 个模块。 searchCtrl.js 是一个控制器,它允许用户进行搜索操作。 searchModule 是一个包含模板的模块。所以也许我在searchCtrl.js 中声明一个模块是错误的?

标签: angularjs angular-ui-router


【解决方案1】:

如下更改 app.js.coffee 的前两个模块声明行。

@myApp  = angular.module('myApp',[]);    
@app    = angular.module('movieseat', ['myApp','ui.router']);

然后在 searchModule.js

angular.module('movieseat').config([

  '$urlRouterProvider', '$stateProvider', function($urlRouterProvider, $stateProvider) {
    $urlRouterProvider.otherwise('/state1');
    return $stateProvider.state('state1', {
      url: '/state1',
      templateUrl: 'assets/angular-app/templates/state1.html'
    });
  }

]);

原因:这里你基本上覆盖了你之前创建的模块,你只需要使用已经声明的模块。

如果你使用的是单个模块,那么

@app    = angular.module('movieseat', ['myApp','ui.router']);

# for compatibility with Rails CSRF protection

@app.config([
  '$httpProvider', ($httpProvider)->
    $httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content')
])

@app.run(->
  console.log 'angular app running'
)

angular.module('movieseat').config([

  '$urlRouterProvider', '$stateProvider', function($urlRouterProvider, $stateProvider) {
    $urlRouterProvider.otherwise('/state1');
    return $stateProvider.state('state1', {
      url: '/state1',
      templateUrl: 'assets/angular-app/templates/state1.html'
    });
  }

]);

你的控制器看起来像

angular.module('movieseat')

  .controller('searchCtrl', [

    '$scope', '$http', function($scope, $http) {

      $scope.search = function() {

        var base = 'http://api.themoviedb.org/3';
        var service = '/search/movie';
        var apiKey = 'a8f7039633f2065942cd8a28d7cadad4&query='
        var search = $scope.searchquery
        var callback = 'JSON_CALLBACK'; // provided by angular.js
        var url = base + service + '?api_key=' + apiKey + search + '&callback=' + callback;

        $http.jsonp(url,{ cache: true}).
          success(function (data, status, headers, config) {

            if (status == 200) {
              $scope.movieList = data.results;
              console.log($scope.movieList)
            } else {
              console.error('Error happened while getting the movie list.')
            }

          }).
          error(function (data, status, headers, config) {
            console.error('Error happened while getting the movie list.')
          });
      }

    }
]);

【讨论】:

  • 谢谢!解释清楚。但我将不得不对整个控制器/模块部分进行更多研究,因为区别并不完全清楚。
  • 酷,很高兴为您提供帮助。如果您在理解这一点上有任何困难,请告诉我。 !!
  • 我肯定会的,现在正在做thinkster.io/angular-rails 教程,所以我猜用不了多久我就会再次陷入困境;)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-18
  • 2016-10-23
  • 2015-10-10
  • 2016-11-12
  • 2016-03-04
  • 2015-11-18
  • 2015-07-27
相关资源
最近更新 更多