简体   繁体   English

Angular指令在动态加载的组件中不起作用

[英]Angular directive not working in dynamically loaded component

I have tried loading the component dynamically, the content is loading but the custom directives are not working I referenced 我尝试动态加载组件,正在加载内容,但是自定义指令不起作用

Dynamically load Angular 4 template using existing component and module 使用现有组件和模块动态加载Angular 4模板

And this reference seems to be old and is overwhelming on how to use jit compiler 而且该参考文献似乎过时了,不知如何使用jit编译器

How can I use/create dynamic template to compile dynamic Component with Angular 2.0? 如何使用/创建动态模板以使用Angular 2.0编译动态组件?

Note the myData which I am getting from backend response is 注意我从后端响应中获取的myData是

   <div xmlns="http://www.w3.org/1999/xhtml" _ngcontent-c18="" 
   appdropzone="" class="dropzone fs-settings__upload-section__floor-wrapper__preview- 
   image__image-area ng-star-inserted" id="toget" ng-reflect-ng-style=" 
   [object Object]"
   style="width: 100%; background-image: 
   url(&quot;data:image/png;base64,iVBORw0KGgoAAAANqyYtn1F
   AhKXkFi/hkQfuCuyO Lfk9ykpOc5CQnOcnR8n/9ySZhLa0Cg==&quot;); background- 
   repeat: no-repeat; background-position: center center; background- 
   size: 
   100% 100%;"><!--bindings={
   "ng-reflect-ng-for-of": ""
   }--><div _ngcontent-c18="" appdroppable="" appmovable="" class="box 
   draggable movable ng-star-inserted" touch-action="none"
  style="transform: translateX(136.8%) translateY(50.4%);"> vav3 </div> 
  <div _ngcontent-c18="" appdroppable="" appmovable=""
  class="box draggable movable ng-star-inserted" touch-action="none" 
  style="transform: translateX(837.6%) translateY(3.20003%);"> vav5 
  </div>
  <div _ngcontent-c18="" appdroppable="" appmovable="" class="box 
  draggable 
  movable
  ng-star-inserted" touch-action="none" style="transform: 
  translateX(639.2%) translateY(340.8%);"> vav54 </div>
  <div _ngcontent-c18="" appdroppable="" appmovable="" class="box 
  draggable movable ng-star-inserted"
  touch-action="none" style="transform: translateX(-288.8%) 
  translateY(276.8%);"> vav4 </div></div>

And the code which I tried with some random html also passing my custom directive in the template, the template loads but the custom directives are not working 我尝试使用一些随机html的代码也在模板中传递了我的自定义指令,该模板已加载,但自定义指令不起作用

ngAfterViewInit() {
 let myData = '<div appMovableArea appDropzone (drop)="move(currentBox, 
    dropzone1)" class="dropzone" style="width:50%; height:50%"></div>'

 const template = myData;

 const tmpCmp = Component({template: template})(class {
 });
 const tmpModule = NgModule({declarations: [tmpCmp]})(class {
 });

 this._compiler.compileModuleAndAllComponentsAsync(tmpModule)
    .then((factories) => {
        const f = factories.componentFactories[0];
        const cmpRef = f.create(this._injector, [], null, this._m);
        //cmpRef.instance.name = 'B component';
        this._container.insert(cmpRef.hostView);
    })
}

The HTML element loads but my custom directives doesn't appear to be working. HTML元素已加载,但我的自定义指令似乎无效。

In your tmpModule you need to add you custom directive declaration 在您的tmpModule中,您需要添加自定义指令声明

NgModule({declarations: [appDropzone, appMovableArea, tmpCmp]})

or import the module that declare this directives 或导入声明此指令的模块

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

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