简体   繁体   English

如何使用 angular 重用 HTML 中的变量

[英]How to reuse a variable in HTML with angular

I'm very new to Angular and devleopping web app.我对 Angular 和开发 web 应用程序非常陌生。

With that said I have a bit of an issue, I have a nested json document with many categories/subcategories and I would like for users to search in this json via a HTML web page with dropdownlists. With that said I have a bit of an issue, I have a nested json document with many categories/subcategories and I would like for users to search in this json via a HTML web page with dropdownlists.

I used this to display the first categories in the dropdown list:我用它来显示下拉列表中的第一个类别:

<select class="custom-select" [(ngModel)]="selectedOption" id='Problem'>
          <option value="" disabled selected="selected"> Pick a problem </option>
          <option *ngFor="let data1 of getKeys(Data)" [ngValue]="data1" id='data1'> {{data1}} </option>
        </select>

But then I need to search into Data (which is my main json file) to get the subcategory.但随后我需要搜索数据(这是我的主要 json 文件)以获取子类别。 This would look like this: Data."what the user picked in the first dropdownlist"这看起来像这样:数据。“用户在第一个下拉列表中选择的内容”

So I tried this:所以我尝试了这个:

<select class='custom-select' [(ngModel)]="selectedOption2" id='Category'>
          <option value="" disabled selected="selected"> pick a category </option>
          <option *ngFor="let data2 of getKeys(Data.selectedOption)" [ngValue]="data2"> {{data2}} </option>
        </select>

Problem is, the html code uses selectedOption as a constant string not as a variable.问题是,html 代码使用 selectedOption 作为常量字符串而不是变量。

My appComponent.ts code looks like this (pretty simple):我的 appComponent.ts 代码如下所示(非常简单):

export class AppComponent {
  
  selectedOption : any;

  selectedOption2 : any;

  title = 'GuideDRC';

  Data = DRCdata;

getKeys(obj){
  return Object.keys(obj)
}

So my question is: is there any way to make the html code "understand" that selectedOption is a variable and not a constant string?所以我的问题是:有没有办法让 html 代码“理解” selectedOption 是一个变量而不是一个常量字符串?

Sorry for this basic question but I'm struggling because many tutorials are with Javascript, thanks in advance !很抱歉这个基本问题,但我很挣扎,因为许多教程都与 Javascript 相关,在此先感谢!

The Data file is json and look something like this:数据文件是 json,看起来像这样:

{
   "First Problem": {
         "First category of first problem": {
                  "To Do" : "Something to do"
                  "Who to call" : "Phone Number"
          "Second category of first problem": {
                   And so on...
   "Second problem":{ ....

It's using "selectedOption" as an index, while you want the contents of your variable selectedOption to be the index.它使用"selectedOption"作为索引,而您希望变量selectedOption的内容成为索引。 The syntax changes:语法变化:

<option *ngFor="let data2 of getKeys(Data[selectedOption])" [ngValue]="data2"> {{data2}} </option>

Also - since selectedOption is initially undefined , this will definitely throw an error.另外 - 由于selectedOption最初是undefined ,这肯定会引发错误。 You can do two things.你可以做两件事。

Only render your second dropdown if selectedOption is set.如果设置selectedOption ,则仅呈现第二个下拉列表。 *ngIf="selectedOption" is enough since it's truthy. *ngIf="selectedOption"就足够了,因为它是真实的。

<select class='custom-select' [(ngModel)]="selectedOption2" id='Category' *ngIf="selectedOption">
          <option value="" disabled selected="selected"> pick a category </option>
          <option *ngFor="let data2 of getKeys(Data[selectedOption])" [ngValue]="data2"> {{data2}} </option>
</select>

Your second option is to set selectedOption initially, and have your getKeys function check if Data contains [selectedOption]您的第二个选项是最初设置selectedOption ,并让您的getKeys function 检查Data是否包含[selectedOption]

selectedOption = ""
// no type needed
getKeys(index: string) {
    return Data.hasOwnProperty(index) ? Data[index] : {};
}

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

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