【问题标题】:Error: [$controller:ctrlreg] after declaring controller?错误:[$controller:ctrlreg] 声明控制器后?
【发布时间】:2017-08-11 04:31:34
【问题描述】:

我已经开始为我参加的 Angular 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>

【问题讨论】:

  • .controller('narrowItDownController ', narrowItDownController ) 删除空格。你看到了吗?
  • 好的,感谢您的帮助。

标签: javascript angularjs


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    .controller('narrowItDownController', narrowItDownController )
    

    你还必须在你的服务中注入$http

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

    【讨论】:

    • 谁投了反对票,请说明原因,以便我改进我的答案。
    • 谢谢,但即使将 $http 注入我的服务后,我仍然会收到错误:[$injector:unpr] 错误?你知道为什么吗?如果可以的话请帮忙。 PS:我不是投反对票的人
    • 你是否删除了 .controller('narrowItDownController' 中的空间?
    • @Vivz 我是投反对票的人。我承认这是一个错误。我只是和$http$https 混淆了,我把它删了。
    • @SankarRaj 没问题。现在你已经给出了反对的理由,这很好
    猜你喜欢
    • 2017-07-19
    • 2020-10-11
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多