【发布时间】:2016-07-07 19:04:50
【问题描述】:
我有一个奇怪的问题,我希望可以解决,但没有解决。
在我的模板中,我使用带有函数调用的表来检索用户名并显示该数据而不是用户的 ID。我已经在没有使用 html 侧调用的情况下单独测试了该函数,并且仅在 java 脚本中它可以工作。但我想知道为什么它不会将数据返回到 html 端。
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.Users = [{
"id": 1,
"display_name": "John Deer",
"role_id": 1,
}, {
"id": 2,
"display_name": "Julie Deer",
}, {
"id": 3,
"display_name": "John Smith",
}];
$scope.Departments = [{
"id": 1,
"created_by_id": 1,
"last_modified_by_id": 2,
"name": "Administrators",
}, {
"id": 2,
"created_by_id": 2,
"last_modified_by_id": 1,
"name": "Guest"
}, {
"id": 3,
"created_by_id": 1,
"last_modified_by_id": 2,
"name": "Manager"
}];
$scope.getUserByID = function(ID) {
console.log('Running getUserByID with: ' + ID + ', as the ID.')
angular.forEach($scope.Users, function(item, index) {
if (ID == item.id) {
console.log('Match is ' + this.Users[index].display_name);
return this.Users[index].display_name;
}
}, $scope);
}
});
/* Put your css in here */
.well {
background-color: lightgrey;
}
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<link data-require="bootstrap-css@3.3.6" data-semver="4.0.0-alpha.2" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" />
<script>
document.write('<base href="' + document.location + '" />');
</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<div class="panel-heading clearfix">
<div class="container col-sm-12">
<div class="col-sm-4">
</div>
<div class="col-sm-4">
<div class="form-group">
<input type="text" class="form-control" ng-model="filterDepartments" value="Search" data-toggle="tooltip" data-placement="top" title="Type here to search in the list." placeholder="Search Here">
</div>
</div>
<div class="col-sm-4">
</div>
</div>
</div>
<div class="col-sm-12 well">
<h3>Departments</h3>
<table id="example" class="display table" style="width: 100%; cellspacing: 0;">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Created By</th>
<th>Modified By</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th>Name</th>
<th>Created By</th>
<th>Modified By</th>
</tr>
</tfoot>
<tbody>
<tr ng-repeat="y in Departments|filter:filterDepartments">
<td>{{y.id}}</td>
<td>{{y.name}}</td>
<td>{{getUserByID(y.created_by_id)}}</td>
<td>{{y.last_modified_by_id}}</td>
</tr>
</tbody>
</table>
</div>
<div class="col-sm-12 panel">
<br>
<br>
</div>
<div class="col-sm-12 well">
<h3>Users</h3>
<table id="example" class="display table" style="width: 100%; cellspacing: 0;">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</tfoot>
<tbody>
<tr ng-repeat="x in Users">
<td>{{x.id}}</td>
<td>{{x.display_name}}</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
我制作了一个 plunk 供你玩:https://embed.plnkr.co/WUBKSD4pBxJnzc9ntUuO/
在我试图找到解决问题的方法时,请告诉我您的想法。
【问题讨论】:
-
两个列标题的任何具体原因 -
ID Name ID 姓名 @Naga Sai A - 这是页眉和页脚。我喜欢这种带有长列表的格式,其中大量数据可能会使疲惫的眼睛感到困惑。这实际上只是我一直以来的偏好。我希望这样做可以帮助那些可能迷路的人哈哈。
标签: html angularjs model-view-controller