【发布时间】: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