我有一个问题,我很困惑在angular js中获取枚举值。好的,我有表列表,列“ Criteria Type”中有mysql的两个值:“ C”是Cost,“ B”是Benefit。但是我的表列表仅显示“ C”或“ B”。那么,如何通过初始字符串“ C”获得/使枚举值成为Cost而使“ B”获得收益呢?
这是我的代码控制器:
adminService.get('kriteria').then(function(response){
$scope.datas = response.data;
});
这是我的显示数据表代码:
<div class = "panel-body">
<div id="add-button"><a href="#!/kriteria/0" class="btn btn-primary" style="margin:5px 5px 5px 10px">Tambah Kriteria</a></div>
<table class="table table-striped">
<thead><tr><th>No</th><th>Code Kriteria</th><th>Nama Kriteria</th><th>Criteria Type</th><th>Skala Kepentingan</th><th>Proses</th></tr></thead>
<tbody><tr ng-repeat="(v,i) in datas"><td>{{v+1}}</td><td>{{i.kd_kriteria}}</td>
<td>{{i.nama_kriteria}}</td><td>{{i.jenis_kriteria}}</td><td>{{i.id_skala}}</td><td><a href="#!/kriteria/{{i.id_kriteria}}">Edit</a> |
<a href="" ng-click="delete(i.id_kriteria)">Hapus</a></td></tr></tbody>
</table>
</div>
代码服务:
/** adminControl Service **/
app.service('adminService', ['$http', '$route', function($http, $route){
var links = "./services/";
this.get = function(url){
return $http({
method:'GET',
url:links + url,
headers: {'Content-Type': 'application/json; charset=utf-8'},
dataType: 'json'
});
}
this.post = function(url,data){
return $http({
method:'POST',
url:links + url,
headers: {'Content-Type': 'application/json; charset=utf-8'},
dataType: 'json',
data: data
});
}
this.update = function(url,data){
return $http({
method:'UPDATE',
url:links + url,
headers: {'Content-Type': 'application/json; charset=utf-8'},
dataType: 'json',
data: data
});
}
this.delete = function(url) {
return $http({
method: 'DELETE',
url: links + url,
headers: {'Content-Type': 'application/json; charset=utf-8'},
dataType: 'json'
});
}
this.getReload = function(url){
$route.reload();
location.href=url;
}
}]);
和我的API在PHP代码:
function kriteria(){
switch($this->get_request_method()){
case "GET":
if(!isset($_GET['id'])){
$result = NULL;
$sql = $this->pdo->prepare("SELECT * FROM tbl_kriteria, tbl_skala
WHERE tbl_kriteria.id_skala = tbl_skala.id_skala ORDER BY 1 DESC");
$sql->execute();
while($r = $sql->fetch(PDO::FETCH_ASSOC)){
$result[] = $r;
}
return $this->response($this->json($result));
}else{
$result = NULL;
$sql = $this->pdo->prepare("SELECT * FROM tbl_kriteria
WHERE id_kriteria = '".$_GET['id']."'
ORDER BY 1 LIMIT 1");
$sql->execute();
if($sql->rowCount()){
$r = $sql->fetch(PDO::FETCH_ASSOC);
$result = $r;
return $this->response($this->json($result));
}
}
break;
case "POST":
$data = json_decode(file_get_contents("php://input"),true);
$sql = $this->pdo->prepare("INSERT INTO tbl_kriteria (id_skala, kd_kriteria, nama_kriteria, jenis_kriteria)
VALUES (:id_skala, :kd_kriteria, :nama_kriteria, :jenis_kriteria)");
$sql->bindParam(':id_skala', $data['id_skala']);
$sql->bindParam(':kd_kriteria', $data['kd_kriteria']);
$sql->bindParam(':nama_kriteria', $data['nama_kriteria']);
$sql->bindParam(':jenis_kriteria', $data['jenis_kriteria']);
$sql->execute();
break;
case 'DELETE':
$sql = $this->pdo->prepare("DELETE FROM tbl_kriteria WHERE id_kriteria = '".$_GET['id']."'");
$sql->execute();
break;
case 'UPDATE':
$data = json_decode(file_get_contents("php://input"),true);
$sql = $this->pdo->prepare("UPDATE tbl_kriteria set kd_kriteria = '".$data['kd_kriteria']."',
nama_kriteria = '".$data['nama_kriteria']."', jenis_kriteria = '".$data['jenis_kriteria']."'
id_skala = '".$data['id_skala']."' WHERE id_kriteria = '".$data['id_kriteria']."'");
$sql->execute();
break;
}
}
最佳答案
如果我理解正确,听起来您想创建一个custom filter。
app.filter('criteriaFilter', function() {
return function(input) {
var myEnum = {
"C": "Cost",
"B": "Benefit",
};
return myEnum[input];
}
});
在您看来:
<li ng-repeat="i in criteria">{{i.type | criteriaFilter}}</li>