简体   繁体   English

在角度 5 中使用组件(HTML)中的指令数据

[英]Use directive data in component (HTML) in angular 5

I am using directive named blue where I have a variable named count .我正在使用名为blue的指令,其中我有一个名为count的变量。 I want to use this count variable in the HTML.我想在 HTML 中使用这个计数变量。 How can I do this?我怎样才能做到这一点? I want something just like below......我想要像下面这样的东西......

<div blue >
{{count}}
</div>

This question is a bit difficult to answer, as there is a lot of Angular knowledge necessary.这个问题有点难回答,因为需要很多 Angular 知识。

But still, to give you some help, there is a good book on Angular from the publisher Springer.但是,为了给你一些帮助,出版商 Springer 出版了一本关于 Angular 的好书。 It's called Pro Angular - Second Edition and the writer is Adam Freeman.它叫做Pro Angular - Second Edition ,作者是 Adam Freeman。 It goes into a lot of detail and also explains everything on directives.它涉及很多细节,还解释了指令中的所有内容。

I a try to give you anwer to your question here, I believe yours might look like this:我试着在这里回答你的问题,我相信你的问题可能是这样的:

<div class="blue">
    {{count}}
</div>

You can either fill the count variable within the Angular template or from the Angular model.您可以在 Angular 模板中或从 Angular 模型中填充count变量。 The model in Angular is typically done using TypeScript. Angular 中的模型通常使用 TypeScript 完成。 Somewhere within that may be a public variable called count or a method called getCount() returning a string.其中某处可能是一个名为count的公共变量或一个名为getCount()的返回字符串的方法。

Check out the mentioned book, as it explains it all.查看提到的书,因为它解释了一切。

In Angular 5, you probably are looking for a component where you can define properties.在 Angular 5 中,您可能正在寻找可以定义属性的组件。

<blue>{{count}}</blue>

https://angular.io/guide/quickstart#first-component , step 4, title in component AppComponent . https://angular.io/guide/quickstart#first-component ,第 4 步,组件AppComponent中的title

Directive is normally used to decorate the behavior, https://angular.io/api/core/Directive .指令通常用于装饰行为, https://angular.io/api/core/Directive Normally I don't think count belongs to blue directive通常我不认为count属于blue指令

<div blue>{{count}}</div>

To do this, you might need Output events.为此,您可能需要输出事件。 Just add @Output() count = new EventEmitter<number>();只需添加@Output() count = new EventEmitter<number>(); in your directive and use it like this:在你的指令中并像这样使用它:

component.html组件.html

<div blue (count)="changeCount($event)">
  {{count}}
</div>

component.ts组件.ts

count: number;
changeCount(event: number) {
  this.count = event;
}

You need to revisit the logic of directive.您需要重新审视指令的逻辑。 for detail this link will help you in detail https://angular.io/guide/attribute-directives有关详细信息,此链接将帮助您详细了解https://angular.io/guide/attribute-directives

You could use output events as LordAlpaca mentioned, or you could pass in an instance of your parent component and have the attribute directive set the count value on your parent component:您可以像 LordAlpaca 提到的那样使用输出事件,或者您可以传入父组件的实例并让属性指令在父组件上设置计数值:

Template:模板:

<h1 [blue]="appComponent">
     {{count}}
</h1>

AppComponent应用组件

export class AppComponent {
    count : number;
    appComponent = this;
}

Directive指示

export class BlueDirective implements OnInit{
  count : number = 4;
  @Input("blue") component : any;

  ngOnInit(){
    this.component.count = this.count;
  }
}

You can achieve this using template var.您可以使用模板变量来实现这一点。 In your directive:在你的指令中:

@Directive({
    selector: '[appBlue]',
    exportAs: 'blueDirective'
})
export class BlueDirective {
    public count: number;
    // your logic here...
}

Implementation looks like this:实现看起来像这样:

<div appBlue #blue="blueDirective">
    {{ blue.count }}
</div>

I hope it can help我希望它可以帮助

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

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