简体   繁体   English

指令上的未知提供程序错误

[英]Unknown Provider Error on Directive

I am receiving an unknown provider error on a directive I made. 我收到有关我发出的指令的未知提供程序错误。 I just began learning about directives so I apologize in advance if I made a very obvious mistake. 我刚刚开始学习指令,因此如果我犯了一个非常明显的错误,我会提前道歉。

The directive is supposed to create an element it creates using a factory. 该指令应该创建一个使用工厂创建的元素。

The div causing the error: 导致错误的div:

<div annotation-display tree="{{tree}}"></div>

The directive that angular cannot find: angular找不到的指令:

angular.module('trees').directive('annotationDisplay', ['annotationFactory', function(annotationFactory) {
    return {
        restrict: 'E',
        scope: {
            tree: '=tree'
        },
        transclude: true,
        compile: function(element, attrs)
        {
            var htmlText = annotationFactory.createAnnotationHtml(tree);
            element.replaceWith(htmlText);
        }
    };
}]);

The factory: 该工厂:

angular.module('trees').factory('annotationFactory', ['', function() {
    var factory = {};
    factory.createAnnotationHtml = function(tree) {
        console.log("in");
        var out = "<p>";
        d3.tsv.parseRows(tree.data, function(data) {
            var previousNERTag = "";
            for (var row in data) {
                var wordData = data[row];
                var NERtag = wordData[7].substring(wordData[7].length,3);
                if (previousNERTag != NERtag) {
                    previousNERTag = NERtag;
                    if (NERtag === "")
                        out+="</span>";
                    if (NERtag === "PER") 
                        out+='<span class="Person">';
                    if (NERtag === "ORG")
                        out+='<span class="Organization">';
                    if (NERtag === "LOC")
                        out+='<span class="Location">';
                }
            }
            out+='</p>';
        });
        return out;
    };

    return factory;
}]);

I loaded the trees module in the main app and its definitely there because my controller in trees is working fine. 我将树模块加载到了主应用程序中,并且肯定在那里,因为树中的控制器运行正常。

Solved, 解决了,

.factory('annotationFactory', ['', function() { to angular.module('trees').factory('annotationFactory', function() { .factory('annotationFactory', ['', function() { to angular.module('trees').factory('annotationFactory', function() {

restrict: 'E' Means your directive is an element. restrict: 'E'您的指令是一个元素。 restrict: 'EA' Means your directive would be an element or an attribute. restrict: 'EA'您的指令将是元素或属性。 As you are using it with div it works as an attribute. div配合使用时,它可以作为属性。 If you want to use it as element then change 如果您想将其用作元素,请更改

<div annotation-display tree="{{tree}}"></div>

to

<annotation-display tree="{{tree}}"></annotation-display>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM