Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 指令已声明并从核心模块导出,但未触发Angular 7_Javascript_Angular_Typescript_Angular Directive - Fatal编程技术网

Javascript 指令已声明并从核心模块导出,但未触发Angular 7

Javascript 指令已声明并从核心模块导出,但未触发Angular 7,javascript,angular,typescript,angular-directive,Javascript,Angular,Typescript,Angular Directive,大家好,我将用几句话继续我的问题。 我在我的核心模块文件夹中创建了一个指令,并将其添加到声明中,然后在这样的模块中导出。该模块在应用程序模块(主体模块)中实现。我在索引页面中使用了指令装饰器(该页面在另一个模块页面模块中声明),一切正常,我没有收到任何错误,但指令的逻辑是nos触发的 我已尝试在页面模块中声明该指令及其工作原理。还有一件很奇怪的事情,如果从核心模块中删除指令,它会抛出错误,无法确定类HasFunctionityDirective的模块是否正确 指示 核心模块 应用程序模块 Ind

大家好,我将用几句话继续我的问题。 我在我的核心模块文件夹中创建了一个指令,并将其添加到声明中,然后在这样的模块中导出。该模块在应用程序模块(主体模块)中实现。我在索引页面中使用了指令装饰器(该页面在另一个模块页面模块中声明),一切正常,我没有收到任何错误,但指令的逻辑是nos触发的

我已尝试在页面模块中声明该指令及其工作原理。还有一件很奇怪的事情,如果从核心模块中删除指令,它会抛出错误,无法确定类HasFunctionityDirective的模块是否正确

指示 核心模块 应用程序模块 Index.html

索引工作!

在这种情况下,元素“p”应将颜色更改为蓝色


正如您所见,应用程序模块接收核心的所有元素,angular不会抛出任何错误,但逻辑不会触发。

您只能在afterviewinit中获取元素的引用。试试这个

import { Directive, ElementRef, Renderer2,  AfterViewInit } from '@angular/core';

@Directive({
  selector: '[appHasfunctionality]'
})
export class HasfunctionalityDirective implements AfterViewInit {


  constructor(
    private elem: ElementRef,
    private renderer: Renderer2
  ) { 
  }

  ngAfterViewInit() {
    this.renderer.setStyle(this.elem.nativeElement, 'color', 'blue');
  }
}

找到了问题的解决方案,我在顶层(main)声明模块,而不是转移到pages模块,因为我意识到这是唯一使用的地方

你能试试下面的
el.nativeElement.style.backgroundColor='blue'
将颜色设置为“blue”吗?嗨,Alexander,你已经试过了,如果我直接在pages模块中声明指令,我显示的代码就可以工作了。我认为这是一种惰性负载?您是否将AppComponent的templateUrl命名为index.html?或者您希望您的指令对index.html起作用吗?您好,yurzui,不,index.html是在页面模块级别创建的。您必须在模块中导入CoreModule,在该模块中声明组件,您尝试使用该指令的模板。Hi jcuypers,我尝试了oninit,afterviewinit,结果是相同的IRD,这基本上是一样的:。顺便说一句,我没有看到BrowserModule的导入。是吗?是的,是相同的,但我在我的应用程序中使用层,如问题中所述,给我时间,我将上传一个带有架构的stackbkitz,确保commonmodule。Browsermodule等需要在应用程序级别加载任何基本的角度工作和thidtoo@jcuypers我这里有些问题,谢谢
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HasfunctionalityDirective } from './directives/hasfunctionality.directive';


@NgModule({
  declarations: [
    HasfunctionalityDirective
  ],
  imports: [
    CommonModule,
  ],
  exports: [
    HasfunctionalityDirective
  ]
})
export class CoreModule { }



import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { CoreModule } from './core/core.module';

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    CoreModule,
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
<p appHasfunctionality>
  index works!
</p>

import { Directive, ElementRef, Renderer2,  AfterViewInit } from '@angular/core';

@Directive({
  selector: '[appHasfunctionality]'
})
export class HasfunctionalityDirective implements AfterViewInit {


  constructor(
    private elem: ElementRef,
    private renderer: Renderer2
  ) { 
  }

  ngAfterViewInit() {
    this.renderer.setStyle(this.elem.nativeElement, 'color', 'blue');
  }
}