gpt4 book ai didi

javascript - 显示前的 AngularJs 数据操作

转载 作者:行者123 更新时间:2023-11-29 16:07:10 25 4
gpt4 key购买 nike

我有一个简单的表格,其中有 2 列:a) 名称 b) 国家。目前它工作正常并显示用户名和国家/地区。

现在我想添加一个条件,如果国家是 Germany 那么它应该打印 Europe 而不是 Germany 而如果国家是 ArgentinaBrazil 它应该打印 South America,对于其他国家/地区它将保持不变,例如 UKSweden或任何其他国家/地区将按原样打印。

我的 Angular 代码是这样的::

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl">

<table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td>{{ x.Country }}</td>
</tr>
</table>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://localhost/test/user_data.php")
.then(function (response) {$scope.names = response.data.records;});
});
</script>

</body>
</html>

我的 user-data.php 文件中的 json 数据是这样的:

{
"records":[
{"Name":"Alfreds Futterkiste","City":"Berlin","Country":"Germany"},
{"Name":"Ana Trujillo Emparedados","City":"México D.F.","Country":"Mexico"},
{"Name":"Antonio Moreno Taquería","City":"México D.F.","Country":"Mexico"},
{"Name":"Around the Horn","City":"London","Country":"UK"},
{"Name":"B's Beverages","City":"London","Country":"UK"},
{"Name":"Berglunds snabbköp","City":"Luleå","Country":"Sweden"},
{"Name":"Blauer See Delikatessen","City":"Mannheim","Country":"Germany"},
{"Name":"Blondel père et fils","City":"Strasbourg","Country":"France"},
{"Name":"Bólido Comidas preparadas","City":"Madrid","Country":"Spain"},
{"Name":"Bon app'","City":"Marseille","Country":"France"},
{"Name":"Bottom-Dollar Marketse","City":"Tsawassen","Country":"Canada"},
{"Name":"Cactus Comidas para llevar","City":"Buenos Aires","Country":"Argentina"},
{"Name":"Centro comercial Moctezuma","City":"México D.F.","Country":"Mexico"},
{"Name":"Chop-suey Chinese","City":"Bern","Country":"Switzerland"},
{"Name":"Comércio Mineiro","City":"São Paulo","Country":"Brazil"}
]
}

最佳答案

有很多方法可以在 Angular 上实现这一点。 (指令、函数、服务、ng-if...等)

在您的情况下,最干净的是自定义过滤器:

.filter('countryFormat', function() {
return function(input) {
if (input != undefined) {
//Your switch case here
return 'South America'; // for example
}
}
});

在 html 中:

<td>{{ x.Country | countryFormat }}</td>

关于javascript - 显示前的 AngularJs 数据操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38321600/

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