简体   繁体   English

Angular 2-基于值的OnClick显示数组

[英]Angular 2 - OnClick show array based on value

I'm working on an Angular 2 project and i'm working with a .json file that looks like this: 我正在Angular 2项目中工作,并且正在使用.json文件,如下所示:

{
    "PropertyName": "Occupation",
    "DefaultPromptText": "occupation text",
    "ValuePromptText": {
        "WebDeveloper": "for web developer",
        "Administrator": "for admin"
    }
},
{
    "PropertyName": "FirstName",
    "DefaultPromptText": "first name text",
    "ValuePromptText": ""
}

I have set up a service that gets this file which looks like this: 我已经设置了获取此文件的服务,如下所示:

import { Injectable } from "@angular/core";
import { Http } from '@angular/http';
import 'rxjs/Rx';

@Injectable()
export class PromptService {

constructor(private http: Http) { }

fetchPrompts() {
    return this.http.get('/temp.json').map(
        (Response) => Response.json()
    );
}

}

My html has form inputs which looks like this: 我的html具有如下形式的表单输入:

<fieldset class="one-quarter sm-padding">
<label>First name</label>
<input name="FirstName" placeholder="Firstname" type="text" tabindex="1" required>
</fieldset>

<fieldset class="one-quarter sm-padding">
<label>Occupation</label>
<input name="Occupation" placeholder="Occupation" type="text" tabindex="2" required>
</fieldset>

<div class="prompt-bubble active md-padding bottom-sm-margin">
<h2>Help Text</h2>
<ul>

    <li *ngFor="let promptText of promptTexts">

        <p>{{promptText.DefaultPromptText}}</p>

    </li>

</ul>

</div>

The component.ts file is this: component.ts文件是这样的:

import { Component, OnInit } from '@angular/core';
import { PromptService } from '../../services/prompt.service';

@Component({
selector: 'home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css'],
providers: [PromptService]
})

export class HomeComponent implements OnInit {


promptTexts = [];

constructor(private promptService: PromptService) { }

ngOnInit() {
    this.promptService.fetchPrompts().subscribe(
        (data) => this.promptTexts = data
    );
}

}

What i'm trying to do is to show a specific array based on if the 'PropertyName' value matches the input name, ie if the 'FirstName' input is clicked the array with the 'PropertyName' that is equal to "FirstName" will show and include the 'DefaultPromptText'. 我想做的是根据'PropertyName'值是否与输入名称匹配来显示特定的数组,即如果单击'FirstName'输入,则具有'PropertyName'的数组将等于“ FirstName”显示并包含“ DefaultPromptText”。

Hope that makes sense. 希望有道理。 Please let me know if you need me to explain anything further. 如果您需要我进一步解释,请告诉我。

Thanks very much 非常感谢

You can try with **ngIf* 您可以尝试使用** ngIf *

<input name="FirstName" [(ngModel)]="firstName" placeholder="Firstname" type="text" tabindex="1">

and

<ul *ngIf="yourFetchData && firstName === promptTexts.PropertyName">
    <li *ngFor="let promptText of promptTexts">
        <p>{{promptText.DefaultPromptText}}</p>
    </li>
</ul>

<ul *ngIf="!yourFetchData || firstName !== promptTexts.PropertyName">
    <li *ngFor="let promptText of promptTexts">
        <p>{{promptText.DefaultPromptText}}</p>
    </li>
</ul>

EDIT: promptTexts is an object. 编辑:提示符文本是一个对象。 So you can't do an *ngFor with it. 因此,您无法使用* ngFor。

so replace: 所以更换:

    <li *ngFor="let promptText of promptTexts">
        <p>{{promptText.DefaultPromptText}}</p>
    </li>

by 通过

   <p>{{promptTexts.DefaultPromptText}}</p>

EDIT2; EDIT2; If you have a list: 如果您有清单:

<ul>
    <li *ngFor="let promptText of promptTexts">
        <p>{{firstName === promptText.PropertyName?promptText.DefaultPromptText:'whatever'}}</p>
    </li>
</ul>

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

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