共享模块中的指令不可见 [英] directive from shared module is not visible

查看:27
本文介绍了共享模块中的指令不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试深入研究 angular 2,对 angular 1 有一些经验并遇到一些难题.

Trying to dive into angular 2 having some experience in angular 1 and having some puzzles.

我制作了一个共享模块:

I made one shared module:

import { NgModule }      from '@angular/core';
import { CommonModule }        from '@angular/common';

import { Constants }   from './injectables/constants';
import { Utils } from "./injectables/utils";
import { HighlightDirective } from "./directives/highlight";

@NgModule({
    imports: [ CommonModule ],
    declarations: [ HighlightDirective ],
    providers: [ Constants, Utils ],
    exports: [ HighlightDirective ]
})
export class VCommonModule { }

如果我错了,请纠正我,但据我所知,这里只需要导出指令、管道和组件?在我将此模块包含到我的 AppModule 的导入中后,可以立即使用服务(可注射)吗?所以我这样做了:

correct me if I'm wrong, but as I understood only directives,pipes and components needs to be exported here? Services(Injectables) could be used straight away after I include this module to imports of my AppModule? So I did that:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { VCommonModule } from './common/module';
import { FormsModule } from "@angular/forms";

import { AppComponent }   from './faq/components/app';
import { SearchComponent }   from './faq/components/search';
import { ArticleComponent } from "./faq/components/article";
import { TopResultsComponent } from "./faq/components/topResults";
import { AjaxService } from "./faq/injectables/ajaxService";
import './rxjs-operators';

@NgModule({
    imports:      [ BrowserModule, FormsModule, HttpModule, VCommonModule ],
    declarations: [ AppComponent, SearchComponent, ArticleComponent, TopResultsComponent ],
    providers: [ AjaxService ],
    bootstrap:    [ AppComponent ]
})

export class AppModule { }

但是当我尝试在我的组件洞察 AppModule 中使用 [highlight] 指令时,它向我显示了一个错误

But when I'm trying to use [highlight] directive in my component insight AppModule it shows me an error

zone.js:388 Unhandled Promise rejection: Template parse errors:
Can't bind to 'highlight' since it isn't a known property of 'span'. ("         <br/>
                <span [innerHTML]="article.Content__c"
                      [ERROR ->][highlight]
                      keywords="test"></span> <!-- [innerHTML]="article.Content__c | "): SearchComponent@39:26 ; Zone: <root> ; Task: Promise.then ; Value: Error: 

来自 VCommonModule 的服务在我将它们添加为 providers: [Constants, Utils ] 后似乎工作正常

services from VCommonModule seems works fine after I added them as providers: [ Constants, Utils ] of my component

import {Directive, Input, ElementRef, Renderer, OnChanges} from "@angular/core";

@Directive({
    selector: '[highlight]'
})
export class HighlightDirective implements OnChanges{
    @Input()
    keywords:string;

    highlightClass: string = 'highLight';

    constructor(
        private elementRef:ElementRef,
        private renderer:Renderer) {
    }

    replacer(match, item) {
        return `<span class="${this.highlightClass}">${match}</span>`;
    }

    tokenize(keywords) {
        keywords = keywords.replace(new RegExp(',$','g'), '').split(',');
        return keywords.map((keyword) => {
            return '\\b'+keyword.replace(new RegExp('^ | $','g'),     '')+'\\b';
        });
    }

    ngOnChanges() {
        if (this.keywords) {
            var tokenized = this.tokenize(this.keywords);
            var regex = new RegExp(tokenized.join('|'), 'gmi');

            var html =     this.elementRef.nativeElement.innerHTML.replace(regex, (match, item) => {
                return this.replacer(match, item);
            });

            this.renderer.setElementProperty(this.elementRef.nativeElement,     'innerHTML', html);
        }
    }
}

PS:角度版本 2.1.2

PS: angular version 2.1.2

推荐答案

您的问题与模块无关;这是模板中使用的语法.

Your problem is not related to the modules; it's the syntax used in the template.

根据错误信息,您使用了 单向绑定语法 - 因为你的 highlight 指令用大括号括起来:

According to the error message, you've used the one-way binding syntax - as your highlight directive is enclosed in braces:

<span ... [highlight] ...></span>

在这种情况下,Angular 将尝试绑定到指令属性或元素属性.您的指令没有名为 highlight 的输入属性,并且 span 元素没有 highlight 属性,因此会产生错误.

In this situation, Angular will attempt to bind to a directive property or to an element property. Your directive has no input property named highlight and the span element has no highlight property, so an error is effected.

如果去掉大括号,问题应该可以解决:

If you remove the braces, the issue should be resolved:

<span ... highlight ...></span>

这篇关于共享模块中的指令不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆