【问题标题】:Display information according to the <selected> location根据 <selected> 位置显示信息
【发布时间】:2020-05-28 16:25:21
【问题描述】:

我正在尝试像一个简单的打印机管理器一样,所以我将能够看到我所有的打印机状态到目前为止我已经设法显示所有数据现在我想按位置和错误来组织它得到的是:无法读取未定义的属性“位置”可能是我认为[(ngModel)]它无法正常工作的原因 谢谢。

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { DomSanitizer } from '@angular/platform-browser';
import { Values } from '../Models/Values';


@Component({
  selector: 'app-value',
  templateUrl: './value.component.html',
  styleUrls: ['./value.component.css']
})


export class ValueComponent implements OnInit {
  selectedPrinter: Values;
  values: any;
  

  constructor(private http: HttpClient, public sanitizer: DomSanitizer) { }



  ngOnInit() {
    this.getValues();


  }

  getValues() {
    this.http.get('http://localhost:5000/api/values/').subscribe(response => {
      this.values = response;
    }, error => {
      console.log(error);
    })
  }

}
<H2>Print Manager</H2>



<div id="selectCompanyDiv">

  <div class="col-12 col-md-3 col-xl-2 mt-5 bd-sidebar">
    <label for="">Select location</label>
    <select class="form-control" [(ngModel)]="selectedPrinter">
      <option *ngFor="let each of values " [ngValue]="each">{{each.location}}</option>
    </select>

  </div>

  <br>
  <br>


  <div>
    <div *ngFor="let value of values" id="mainDiv">
      <!-- <p>{{value.id}},{{value.hostName}},{{value.location}},{{value.manufacturer}},{{value.ip}}</p> -->

      <span>Hostname: {{value.hostName}}</span>
      <br>
      <span>Location: {{value.location}}</span>
      <br>
      <span>Manufacturer: {{value.manufacturer}}</span>
      <br>
      <span>IP: {{value.ip}}</span>
      <br>
      <h2>{{value.location}}</h2>


      <div>

        <div *ngIf="value.hostName==='BROTHERJ480'" class="outerFrame">
          <iframe [src]="sanitizer.bypassSecurityTrustResourceUrl('http://'+value.ip+'/general/status.html')" id="inneriframej480" scrolling="no"></iframe>
        </div>

        <div *ngIf="value.hostName==='Brother6530DW'" class="outerFrame">
          <iframe [src]="sanitizer.bypassSecurityTrustResourceUrl('http://'+value.ip+'/general/status.html')" id="inneriframeBrother6530DW" scrolling="no"></iframe>
        </div>


      </div>

    </div>

【问题讨论】:

  • 你能分享你从 api 得到的东西吗?你想用选择方法做什么?它现在什么都不做
  • 我正在获取 json 的:id、主机名、位置、ip 和制造商
  • 你想对选择元素做什么?
  • 我正在尝试按位置获取选定的打印机,例如,如果我选择厨房,那么我只会看到厨房打印机
  • Okey 我添加了答案,您需要一个 if 条件来检查值是否等于所选值 @Chen Tmv

标签: javascript html asp.net angular typescript


【解决方案1】:

您需要 if 条件来显示与所选值相关的隐藏

在组件变化中

selectedPrinter: string="Bed Room";

然后在html中

<H2>Print Manager</H2>
<div id="selectCompanyDiv">
  <div class="col-12 col-md-3 col-xl-2 mt-5 bd-sidebar">
    <label for="">Select location</label>
    <select class="form-control" [(ngModel)]="selectedPrinter">
      <option *ngFor="let each of values " value="{{each.location}}">{{each.location}}</option>
    </select>
  </div>
  <br>
  <br>
<div>
<div *ngFor="let value of values" id="mainDiv">
    <div *ngIf="value.location===selectedPrinter">
      <span>Hostname: {{value.hostName}}</span>
      <br>
      <span>Location: {{value.location}}</span>
      <br>
      <span>Manufacturer: {{value.manufacturer}}</span>
      <br>
      <span>IP: {{value.ip}}</span>
      <br>
      <h2>{{value.location}}</h2>
      <div>
        <div *ngIf="value.hostName==='BROTHERJ480'" class="outerFrame">
          <iframe [src]="sanitizer.bypassSecurityTrustResourceUrl('http://'+value.ip+'/general/status.html')" id="inneriframej480" scrolling="no"></iframe>
        </div>
        <div *ngIf="value.hostName==='Brother6530DW'" class="outerFrame">
          <iframe [src]="sanitizer.bypassSecurityTrustResourceUrl('http://'+value.ip+'/general/status.html')" id="inneriframeBrother6530DW" scrolling="no"></iframe>
        </div>
      </div>
    </div>
</div>

【讨论】:

  • 我在主机名下得到了红色下划线,它假设是主机名(就像在我的模型中一样),当我将它更改为主机名时,我获取无法读取未定义的属性“主机名”
  • 好吧,我更新了答案,我错过了您对 selectedPrinter 的价值。这是一个位置值@Chen Tmv
  • 是的,现在我根本看不到 iframe,即使点击时也看不到....xD
  • 是的!终于成功了!非常感谢你,你知道为什么选择是空白的吗?
【解决方案2】:

您可以在选择时使用ngModelChange 事件获取选择的值,并且可以执行所需的功能(基于选择的打印机值)。如果这不是问题,请详细说明问题。

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签