如何将字符串发送到 angular4 指令?

How to send a string to an angular4 directive?

我遇到了这个问题,我有一个 Web 应用程序,我有一个 html 页面,实际上是两个页面,现在我制作了一个指令 (appResizer),它适用于我的两个页面,并且为了改进我的代码,我想从 html 页面向指令发送一个字符串。你能帮我吗?

这是我的 2 个页面中的代码:

<div appResizer>
...
</div>

基本上,它们都有一个很大的 div,如果需要我可以显示或隐藏它。

然后我得到了指令:

import { Directive, ElementRef, Input, Renderer, OnInit, HostListener, 
Output, EventEmitter } from '@angular/core';

@Directive( {
    selector: '[appResizer]'
} )
export class ResizerDirective implements OnInit { 
    // 
}

我需要以某种方式向指令发送一个 sting,作为输入或其他... 请帮忙

你试过这个吗:

在 html 页面中:

<div [appResizer]="'yourStringToSend'"

并且在指令中:

@Input() appResizer: string;

这是一种方法。

另一种是,要创建一个全局服务,在服务中添加一个变量,显示您要发送到指令的字符串,然后像这样使用它:

在glolobalService.ts中创建一个变量:

public myStringToSend: string;

在指令中:

import { GlobalService } 'global.service';

并像在 angular4 环境的其他部分一样正常使用它。

console.log(this.globalService.myStringToSend);

祝你好运。