【问题标题】:"ctrls is not defined" error not sure where to define it“未定义 ctrls”错误不确定在哪里定义它
【发布时间】:2016-07-09 17:55:46
【问题描述】:

我收到 CTRLS is not defined 错误,我不知道在哪里定义 ctrls。我完全是 angularJS 的菜鸟,我正在尝试调用烂番茄 API 来搜索电影。我将在哪里定义 CTRLS,我将如何编写代码?

angular.module('demoApp',[])
  .constant('apiKey', 'removed for security' )
  .constant('http://api.rottentomatoes.com/api/public/v1.0')

document.getElementById('searchBox').addEventListener('keydown', function (event) {
    if (event.which === 13 || event.keyCode === 13) {

        // construct the uri with our apikey
        var searchText = this.value;
        console.log('Enter works');

        ctrls.controller('resultsCTRL', function ($scope, $http) {
            $scope.search = searchText;
            console.log('control function works');
            $http.jsonp('http://api.rottentomatoes.com/api/public/v1.0/movies.json', {
                params: {
                    q: 'toy',
                    page_limit: 10,
                    page: 1,
                    apikey: apiKey,
                    callback: 'JSON_CALLBACK'                    
                }
            });
        });
    };
});

【问题讨论】:

  • ctrls 未定义。

标签: javascript angularjs


【解决方案1】:

ctrls 未定义。所以你需要定义引用哪个模块。

这应该可以帮助您:https://docs.angularjs.org/guide/controller

此处复制的示例供参考:

var myApp = angular.module('myApp',[]);

myApp.controller('DoubleController', ['$scope', function($scope) {
  $scope.double = function(value) { return value * 2; };
}]);

<div ng-controller="DoubleController">
  Two times <input ng-model="num"> equals {{ double(num) }}
</div>

【讨论】:

    【解决方案2】:

    您使用 ctrls 的方式就像您最初将 AngularJS 模块存储到变量中一样。

    例如:

    var ctrls = angular.module('demoApp', []);
    
    ctrls
      .constant('apiKey', 'removed for security' )
      .constant('http://api.rottentomatoes.com/api/public/v1.0')
    
    ctrls.controller(function($scope, $http){
    //Logic Here
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-06
      • 1970-01-01
      相关资源
      最近更新 更多