2016-03-22 82 views
0

我得到一个CTRLS是未定义的错误,我不知道在哪里定义ctrls。我是一个完整的noob到angularJS,我想调用烂番茄API来搜索电影。我将在哪里定义CTRLS,以及如何编写代码?“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'      
       } 
      }); 
     }); 
    }; 
}); 
+1

'ctrls'没有定义。 –

回答

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> 
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 
});