“ctrls未定义”错误,不确定在何处定义它

时间:2016-03-22 17:28:58

标签: javascript angularjs

我得到一个CTRLS没有定义错误,我不知道在哪里定义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'                    
                }
            });
        });
    };
});

2 个答案:

答案 0 :(得分:0)

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>

答案 1 :(得分:0)

您使用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
});