You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 4中通过TypeScript访问JSON对象并在HTML展示的方法

Hey there! Let's break down how to display your CandidateSchema data on an HTML page using TypeScript—super straightforward once you map out the steps. Here's a step-by-step implementation approach:

1. Define TypeScript Interfaces for Type Safety

First, let's create a TypeScript interface to match your JSON structure. This gives you type hints and catches errors early if your data shape changes:

interface CandidateSchemaRow {
  Description: string;
  Experience: number;
  type: string;
}

interface CandidateSchema {
  CandidateSchemaRows: CandidateSchemaRow[];
}
2. Set Up Your HTML Container

Add a container element in your HTML where we'll inject the rendered select boxes:

<div id="candidate-form-container"></div>
3. Write the TypeScript Rendering Logic

We'll wait for the DOM to fully load, then iterate over your data to generate and append the select boxes to the container. Here's the full logic:

// Wait for the DOM to be ready before making changes
document.addEventListener('DOMContentLoaded', () => {
  // Your raw JSON data (could also fetch this from an API later)
  const candidateData: CandidateSchema = { 
    "CandidateSchemaRows": [ 
      { "Description": "sadasd", "Experience": 1, "type": "selectbox" }, 
      { "Description": "erwerw", "Experience": 2, "type": "selectbox" } 
    ] 
  };

  // Grab the container element from the HTML
  const container = document.getElementById('candidate-form-container');
  if (!container) return; // Exit early if the container doesn't exist

  // Loop through each row in the schema
  candidateData.CandidateSchemaRows.forEach((row, index) => {
    // Create a wrapper div to group the label and select box
    const rowWrapper = document.createElement('div');
    rowWrapper.style.marginBottom = '1.5rem';

    // Create a label for the description
    const label = document.createElement('label');
    label.textContent = row.Description;
    label.htmlFor = `experience-select-${index}`; // Unique ID for each select
    rowWrapper.appendChild(label);

    // Generate the select box (since all rows are type "selectbox")
    if (row.type === 'selectbox') {
      const selectBox = document.createElement('select');
      selectBox.id = `experience-select-${index}`;

      // Add experience options (adjust the range based on your needs)
      for (let year = 1; year <= 5; year++) {
        const option = document.createElement('option');
        option.value = year.toString();
        option.textContent = `${year} year(s) of experience`;
        
        // Set the default selected option to match the row's Experience value
        if (year === row.Experience) {
          option.selected = true;
        }

        selectBox.appendChild(option);
      }

      rowWrapper.appendChild(selectBox);
    }

    // Add the completed row to the container
    container.appendChild(rowWrapper);
  });
});

To run this, you'll need to compile your TypeScript to JavaScript:

  • Install TypeScript globally: npm install -g typescript
  • Compile your file: tsc your-file-name.ts (this creates a .js file)
  • Link the compiled JS in your HTML:
<script src="your-file-name.js"></script>

Alternatively, if you're using a dev server (like Vite or Live Server), you can use ES modules:

  • Set "module": "ESNext" in your tsconfig.json
  • Link the TypeScript file directly as a module:
<script type="module" src="your-file-name.ts"></script>

Key Notes

  • The interface ensures you're working with the correct data properties—if you accidentally misspell Experience as experience, TypeScript will flag it immediately.
  • We're using unique IDs for each select box to keep labels linked properly (accessibility win!).
  • You can easily extend this logic to handle other type values (like text inputs) by adding more conditionals in the loop.

内容的提问来源于stack exchange,提问作者roopteja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:13:29