简体   繁体   English

Angular-自定义结构指令-ngForIn:如何创建自己的局部变量?

[英]Angular - Custom Structural Directive - ngForIn: How can I create my own local variable?

I hope you understand this correctly. 希望您能正确理解。

I created a custom ngForIn directive to get the keys of an object. 我创建了一个自定义ngForIn指令来获取对象的键。 This works properly with the following code: 可以使用以下代码正常工作:

import {Directive, Input, OnChanges, SimpleChange, SimpleChanges} from "@angular/core";
import {NgForOf} from "@angular/common";

@Directive({
    selector: "[ngFor][ngForIn]"
})
export class NgforinDirective<T> extends NgForOf<T> implements OnChanges {

    @Input() public ngForIn: any;

    public ngOnChanges(changes: SimpleChanges): void {
        if (changes.ngForIn) {

            if (this.ngForIn) {
                this.ngForOf = Object.keys(this.ngForIn) as Array<any>;
                const change = changes.ngForIn;
                const currentValue = Object.keys(change.currentValue);
                const previousValue = change.previousValue ? Object.keys(change.previousValue) : undefined;
                changes.ngForOf = new SimpleChange(previousValue, currentValue, change.firstChange);
            }
            super.ngOnChanges(changes);
        }
    }
}

The problem I'm facing right now is that I need to have access to the value of object[key] multiple times in the template. 我现在面临的问题是,我需要多次访问模板中的object [key]的值。 As of now i'm working with it like this (dirty): 到目前为止,我正在使用它(脏):

<div *ngFor="let key in object">
     {{object[key]}}

However, I would like to be able to do something like this: 但是,我希望能够执行以下操作:

<div *ngFor="let key in object; let value = object[key]">
     {{value}}

I have being reading the source code for ngForOf as it includes some local variables as "index" or "odd". 我正在阅读ngForOf的源代码,因为它包含一些局部变量,例如“ index”或“ odd”。

I think a solution would be to create a local variable within the custom directive that would return the value of the object[key] that is iterating at that moment, but I don't really understand how should I do it; 我认为一种解决方案是在自定义指令中创建一个局部变量,该局部变量将返回当时正在迭代的object [key]的值,但是我真的不知道该怎么做。 or maybe there is an easier solution that I'm not aware of. 也许有一个我不知道的更简单的解决方案。

Has anyone faced a problem like this and found a solution? 有谁遇到过这样的问题并找到了解决方案?

Thanks! 谢谢!

You can already do what you want using *ngFor . 您已经可以使用*ngFor做您想做的*ngFor Just get the keys of the object using Object.keys(yourObject) , then iterate over them and display the value. 只需使用Object.keys(yourObject)获得对象的键,然后对其进行迭代并显示值。

import { Component, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

@Component({
  selector: 'my-app',
  template: `
    <ul>
      <li *ngFor="let key of keys;">
        {{ key }}: {{ person[key] }}
      </li>
    </ul>
  `,
})
export class App {
  keys: string[];
  person: object;

  constructor() {
    this.person = {
      'First Name': 'Bob',
      'Last Name': 'Smith',
      'Age': 44,
    };
    this.keys = Object.keys(this.person);
  }
}

@NgModule({
  imports: [BrowserModule],
  declarations: [App],
  bootstrap: [App],
})
export class AppModule {}

Plunkr: https://plnkr.co/edit/BOgMT8XYphirNi6kObZv Plunkr: https ://plnkr.co/edit/BOgMT8XYphirNi6kObZv

You could also create a Pipe to get the object keys if you didn't want to do that in your component. 如果不想在组件中这样做,也可以创建一个Pipe以获取对象键。 I'm not sure what problem accessing object[value] presents. 我不确定访问object[value]遇到什么问题。

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

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