angular - 将 ngfor 值传递给图像

标签 angular dart ngfor

我的 JSON 字符串值如下所示,我正在将国家名称加载到 Select 控件中。这工作正常,现在无论何时选择一个国家,相关图像都应传递给 IMG 标签示例,如果选择国家印度,则图像应该是这样的

<img src="india.png">

如果选择了英格兰,那么图像标签应该
<img src="hello.png"> 

下面是我从 Web 服务调用中获得的 JSON 值。
[{
"Name": "England ",
"ImageFile": hello.png,
"Id": 1
},
{
"Name": "South Africa",
"ImageFile": test.png,
"Id": 2
}, 
{
"Name": "India",
"ImageFile": india.png,
"Id": 2
}]

在 Dart 中,我编写了获取 Country Id 的函数,该值在 dart 函数中正确出现。现在我需要根据所选国家更改图像值
   <label style="alignment: center" for="countries">Country
        <select class="form-control" id="countries" *ngIf="sCountries.length > 0" [ngModel]="0" (ngModelChange)="selectCountry($event)">
            <option value="0">All</option>
            <option *ngFor="let country of sCountries" value="{{country['Id']}}">{{country['Name']}}</option>
        </select>
    </label>

谁能帮我。

最佳答案

您可以将整个国家/地区对象与 ngModel 绑定(bind),这不需要您手动遍历循环来获取选定的国家/地区对象。双向绑定(bind)功能强大。

<label style="alignment: center" for="countries">Country
    <select class="form-control" id="countries" *ngIf="sCountries.length > 0" [(ngModel)]="selectedCountry">
        <option *ngFor="let country of sCountries" [value]="country">{{country['Name']}}</option>
    </select>
</label>

<ng-container *ngIf="selectedCountry"> 
    <img [src]="selectedCountry.ImageFile">
</ng-container>

关于angular - 将 ngfor 值传递给图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56521171/

相关文章:

Angular 2 : Is it possible to use custom types in html templates?

Dart 不变性

angular - 如何在ngFor中以angular 2绑定(bind)img src?

angular - 在 Angular 中更改页面时表格不显示数据

css - 如何正确覆盖 Angular 库的变量

php - 是否可以使用 Webpack、Sass、PHP 和 MySQL 来运行 Angular?

dart - 当 Flutter 的 TabBar 被点击时移除高亮

dart - 导入的库 'package:geolocator/model/position.dart' 不能有 part-of 指令

javascript - Angular 2 - NgFor 不更新 View

javascript - 没有 `*` 的结构指令?