作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个问题,我对在 Angular js 中获取枚举值感到困惑。好吧,我有表列表,“条件类型”列有来自 mysql 的两个值:“C”是成本,“B”是 yield 。但我的表格列表只显示“C”或“B”。那么,我如何通过初始字符串“C”获取/使枚举值成为成本,将“B”设为 yield ?这是我的代码 Controller :
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;
}
}]);
以及我的 php 代码中的 API:
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>
关于javascript - 如何使用响应数据获取 Angular js中的枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41906647/
我是一名优秀的程序员,十分优秀!