Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.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
Angular 调用服务方法的指令_Angular_Angular6_Angular7 - Fatal编程技术网

Angular 调用服务方法的指令

Angular 调用服务方法的指令,angular,angular6,angular7,Angular,Angular6,Angular7,我在Angular 7中创建了一个模式服务,如下所示: 是否可以创建一个指令来打开模态并按如下方式使用: <button open-modal="HelloComponent">Open Modal</button> <button openModal [modalIdentifier]="'helloComponent'">Open Modal</button> 不确定我发布的语法…您可以通过创建自定义指令来实现所需的行为: open-mod

我在Angular 7中创建了一个模式服务,如下所示:

是否可以创建一个指令来打开模态并按如下方式使用:

<button open-modal="HelloComponent">Open Modal</button>
<button openModal [modalIdentifier]="'helloComponent'">Open Modal</button>

不确定我发布的语法…

您可以通过创建自定义指令来实现所需的行为:

open-modal.directive.ts:

虚拟开放模式.service.ts

该指令可以这样使用:

<button open-modal="HelloComponent">Open Modal</button>
<button openModal [modalIdentifier]="'helloComponent'">Open Modal</button>

此示例为and。

您可以直接调用该方法,而不用使用指令。你可以保持简单。指令是多个组件中使用的常用布局。

为什么要使用指令而不是使用参数直接调用方法?比如:…不需要在我使用它的所有组件中注入ModalService。。。让事情变得更简单,不是吗?我尝试了你的建议,但当使用指令时,我得到了错误:错误:没有为HelloComponent找到组件工厂。您是否将其添加到@NgModule.entryComponents?。但在我的应用程序模块中,我有:EntryComponent:[HelloComponent]。在按钮中调用服务时,我没有收到错误。这是我的更新代码:。你知道我遗漏了什么吗?这是一个与动态组件相关的无关问题,我建议为其创建另一个问题。我在stackblitz项目中修复了许多与动态组件相关的问题,请在创建新问题时通知我。我刚才在这里做了:
@Injectable({
  providedIn: 'root'
})
export class ModalService {

  constructor() {}

  openModal(modalIdentifier: string) {
    console.log(modalIdentifier)
  }
}
<button openModal [modalIdentifier]="'helloComponent'">Open Modal</button>