gpt4 book ai didi

angularjs - 在 *ngIf 中调用函数

转载 作者:行者123 更新时间:2023-12-02 23:22:07 26 4
gpt4 key购买 nike

我遇到这种情况,我想调用一个函数来显示正确的 html 标记。这是我当前的代码:

<li ng-repeat="data in myBlock.myData">
<div ng-if="data.type=='Jonh'">
<div class='row'>
<div class='col'>
this is text for Jonh
</div>
</div>
</div>
<div ng-if="data.type=='Roy'">
<div class='row'>
<div class='col'>
Roy will have a different text
</div>
</div>
</div>
<div ng-if="data.type=='Kevin'" >
<div class='row'>
<div class='col'>
Kevin text is also different
</div>
</div>
</div>
</li>

我想将所有 ngif 合并到一个名为“showField(name)”的函数中,该函数将检查并返回正确的 html 标记。怎么可能呢?我的最终 html 标记应如下所示:

<li ng-repeat="data in myBlock.myData">
<div *ngIf="showField(data.type)">
//Not sure how to implement this part
</div>
</li>

最佳答案

HTML:

<li ng-repeat="data in myBlock.myData">
<div ng-if="checkValidDataType(data.type)">
<div class='row'>
<div class='col'>
<label class='title' for="{{data.name}}">{{data.label}}</label>
<span class="{{data.type.toLowerCase() + 'Class'}}">This is {{data.type}}</span>
</div>
</div>
</div>
</li>

JS:

$scope.checkValidDataType = function(type) {
return type === "Roy" || type === "Kevin" || type === "Jonh";
}

关于angularjs - 在 *ngIf 中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45789851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com