我正在尝试用字符串“Yup”和“”替换表中的数据:如果字符串等于“yes”,则显示“Yup”,否则不显示,抱歉,我是新手,我看到了一些解决方案,我尝试了这个但不起作用: {{ person.value ? “是的”:“”}}
..请提供帮助
angular.module('selectExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.register = {
value: [
{value:"Yes"},{value:"no"},{value:"yes"},
{value:"No"},{value:"no"}
],
};
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="selectExample" ng-controller="ExampleController">
<table id="example" width="100%">
<thead>
<tr align="center">
<th>Value</th>
<th>Replace</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="person in register.value">
<td align="center">{{ person.value }}</td>
<td align="center">{{ person.value ? "Yup":" "}}</td>
</tr>
</tbody>
</table>
</div>
最佳答案
如果您无法更改您的值,您可以调用 toLowerCase()
检查该值以确保其为小写,然后将其与 "yes"
进行比较
angular.module('selectExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.register = {
value: [
{value:"Yes"},{value:"no"},{value:"yes"},
{value:"No"},{value:"no"}
],
};
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="selectExample" ng-controller="ExampleController">
<table id="example" width="100%">
<thead>
<tr align="center">
<th>Value</th>
<th>Replace</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="person in register.value">
<td align="center">{{ person.value }}</td>
<td align="center">{{ person.value.toLowerCase() == "yes" ? "Yup":" "}}</td>
</tr>
</tbody>
</table>
</div>
您的代码无法正常工作的原因是当您使用三元 (? :)
时它将值转换为真值,并且非空字符串将始终为真,因此每个值都是真值并且始终为 "Yup"
关于javascript - 如果值= ="string",如何替换 ng-repeat 中的值,否则不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55746573/