javascript - 指令通信时出错

标签 javascript html css angularjs angular-directive

当我包含另一个指令时出现错误,我做错了什么请帮忙。 这是我收到的错误

Error:- [$compile:ctreq] Controller 'mthelp', required by directive 'getf', can't be found!

我的两个指令:-

1)

app.directive('mthelp', ['$parse', '$http','$filter', function ($parse, $http,$filter) {
    return {
        restrict: 'AE',
        scope: {},
        templateUrl: 'dropDownTable.html',
        controller : function ($scope) {
            console.log(element);
            this.tests = "s";
        }
    }
}]);

2)

app.directive('getf', function () {
    return {
        require: 'mthelp',
        scope: {},
        link: function (scope, element, attr, mthelpCtrl) {
            console.log(element);
            if(event.which === 114 || event.which === 32)
            {
                console.log(mthelpCtrl.tests);

            }
        }
    };
});

我的 html 我是怎么称呼他们的。

        <div class="form-group">
            <label class="col-sm-3">FirstName</label>
            <div class="col-sm-9">
                <input type="text" getf ng-model="firstText" hpcode="1">
            </div>
        </div>


  <div class="col-xs-4 pull-right" mthelp  donotapply=true></div>

这段代码有没有错误,谁能帮忙,谢谢? 我对这个指令有点困惑。

最佳答案

如果遇到指令 mybe 需要或不需要此指令的情况,请尝试像这样将其设置为可选。

app.directive('getf', function () {
    return {
        require: '?mthelp', // add ? before directive name 
        scope: {},
        link: function (scope, element, attr, mthelpCtrl) {
            console.log(element);
            if(event.which === 114 || event.which === 32)
            {
                console.log(mthelpCtrl.tests);

            }
        }
    };
});

关于javascript - 指令通信时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40881424/

相关文章:

html - 将一个 DIV 放置在另一个 DIV 的底部和中心,响应地

javascript - 在 wordpress 上使用自定义 CSS 制作动画

javascript - for循环的两个计数?

html - 使 div 之间以及容器边缘之间的距离相等

javascript - 告诉浏览器为同一域下的每个路径单独存储不同的密码

javascript - 让 child ul in an li

jquery - 如何使用 jQuery 使一个元素在悬停另一个元素时具有 'hover' 效果(CSS)?

javascript - 上传文件Ext Js 2.2

javascript - $.submit() 不工作

javascript - 隐藏或删除 html 加载器?