声明控制器后出现错误:[$ controller:ctrlreg]?

时间:2017-08-11 04:31:34

标签: javascript angularjs

我已经开始为我所在的角度js课程编写一些代码,我在控制台日志中遇到此错误(错误:[$ controller:ctrlreg]),我不知道为什么?我在angular的网站上查看了它,它说你试图调用一个不存在的控制器,或者沿着这些线路调用的东西。我检查过并且我已经声明了控制器,所以我不知道为什么会出现这个错误。这是我的代码,请帮忙谢谢!

JS

(function () {
      'use strict';

      angular.module('narrowDownMenuApp', [])
      .controller('narrowItDownController ', narrowItDownController )
      .service('MenuSearchService', MenuSearchService);

      narrowItDownController.$inject = ['MenuSearchService'];
      function narrowItDownController(MenuSearchService) {
        var menu = this;
        menu.input = "";
        menu.search = function() {
          console.log("blah");
          MenuSearchService.getMatchedMenuItems(searchTerm);
        }
      }
      MenuSearchService.$inject = ['$https'];
      function MenuSearchService($https) {
        var service = this;
        service.getMatchedMenuItems = function(searchTerm) {
          return $http({
            method: "GET",
            url: ('https://davids-restaurant.herokuapp.com/menu_items.json')
          }).then(function (result) {
              console.log(result);
            var foundItems

            // return processed items
            return foundItems;
          });
        }
      }
    })();

HTML

<!doctype html>
<html ng-app="narrowDownMenuApp">

<head>
    <title>Narrow Down Your Menu Choice</title>
    <meta charset="utf-8">
    <script src="angular.min.js"></script>
    <script src="app.js"></script>
</head>

<body>
    <div ng-controller='narrowItDownController as menu'>
        <h1>Narrow Down Your Chinese Menu Choice</h1>

        <div>
            <input type="text" placeholder="search term" ng-model="menu.input"> {{menu.input}}
        </div>
        <div>
            <button ng-click="menu.search()">Narrow It Down For Me!</button>
        </div>

        <!-- found-items should be implemented as a component -->
        <found-items found-items="...." on-remove="...."></found-items>
    </div>

</body>

</html>

2 个答案:

答案 0 :(得分:1)

定义中控制器名称中有空格。删除它,你的控制器将工作

 angular.module('narrowDownMenuApp', [])
      .controller('narrowItDownController', narrowItDownController )

答案 1 :(得分:0)

你有一个空间是你的控制器初始化

.controller('narrowItDownController', narrowItDownController )

您还必须在服务中注入$http

 MenuSearchService.$inject = ['$http'];
 function MenuSearchService($http) {
     ....
 }