我尝试使用 ng-if 在列表中选择一个项目来了解是否使用 ng-if 定义了该项目 这是我的 Angular 代码
function ($scope) {
var dirlist = this;
dirlist.list = [{
name: 'laugh',
time: 10
},
{
name: 'eat',
time: 12
}, {
name: 'work out',
time: 11
}, {
name: 'make something',
time: 13
}];
}
我想问一下列表中是否包含一个叫heo的名字 使用以下命令为其添加声音 html
<div ng-if="dirlist.list.indexof(hio)!== -1 " class="list-item">
<audio controls autoplay>
<source src="audio\Hio Hio Hio.mp3" type="audio/mpeg">
</audio>
</div>
最佳答案
您需要编写一个函数来检查数组中每个对象的 name 属性。如果找到“hio”则返回true。
function checkHio(){
for (var i=0; i < dirlist.list.length; i++) {
if (dirlist.list[i].name === 'hio') {
return true;
}
}
return false;
}
然后你可以在 ng-if 指令中调用这个函数。
<div ng-if="checkHio()" class="list-item">
<audio controls autoplay>
<source src="audio\Hio Hio Hio.mp3" type="audio/mpeg">
</audio>
</div>
关于javascript - 如何使用 ng-if 显示列表中是否定义了某个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41896380/