在指令错误中找不到控制器

时间:2014-06-29 07:39:55

标签: angularjs angularjs-directive

我在控制台中收到以下错误:

  

错误:[$ compile:ctreq]控制器' gamesList',指令要求   ' searchTerm',无法找到!

以下是我使用的代码:

游戏列表:

<div class = "bit-75-percent games-list" data-ng-controller = "GamesController">
    <ng-include src = "'../templates/simpleSearch.tpl.html'"></ng-include>
    <div class = "game" data-ng-repeat = "game in games" data-games-list>
        {{game.name}}
    </div>
</div>

搜索表单

<form class = "simple-search" data-ng-controller ="SearchController">
    <input type = "text" name = "search-term" class = "search-term" data-search-term>
    <div data-pane></div>
</form>

奥运会指令

(function(window, angular, undefined) {
    var app;

    app = angular.module('games');

    app.directive('gamesList', function(GamesService) {
        console.log('Loaded gamesList directive');
        return {
            controller: function(scope) {
                scope.updateGamesList = function(gamesList) {
                    scope.games = gamesList;
                };
            },
            restrict: 'A',
            scope: {},
            transclude: true
        };
    });
}(window, window.angular));

搜索指令:

(function(window, angular, undefined) {
    var app;

    app = angular.module('search');

    app.directive('searchTerm', function(GamesService) {
        console.log('Loaded searchTerm directive');
        return {
            restrict: 'A',
            require: 'gamesList',
            scope: {},
            transclude: true,
            link: function(scope, element, attr) {
                element.on('keyup', function() {
                    GamesService.query();
                });
            }
        };
    });
}(window, window.angular));

我尝试和/或双重检查

  1. 在指定scope之间交替,而不是为每个指令指定。
  2. 确保在gamesList
  3. 之前加载searchTerm指令
  4. 确保两个指令都已加载。
  5. 我不确定我在这里做错了什么。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

指令定义对象的require属性允许确保您在同一元素或其父元素中指定了指令。它不能用于检查页面上任何地方是否存在指定的指令。

BTW,控制器的参数由$injector服务解析,应该正确命名:$scope但不是scope