【发布时间】:2017-05-17 03:54:05
【问题描述】:
我正在尝试创建一个显示建筑物的单页应用程序(使用 ng-sho 和 ng-hide)。目前,我的代码如下所示
index.html
<html ng-app="myApp">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<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.12/angular.js" data-semver="1.4.9"></script>
<script src="app.js"></script>
<title>Happy Building Company</title>
</head>
<body ng-controller="MainCtrl">
<!--BUILDING DIRECTORY-->
<div id="directory" class="mainDiv" ng-show="buildingDirVisible">
<form>
<fieldset>
<legend>Building Directory</legend>
<div id="directoryTable" style="overflow-x:auto;">
<table class="table table-responsive" id="dirtable">
<tr>
<th>Building ID</th>
<th>Owner</th>
<th>Address</th>
<th>View</th>
</tr>
<tr class="rowHover" ng-repeat="item in buildings" >
<td> {{item.ID}}</td>
<td> {{item.Owner}}</td>
<td> {{item.Address}}</td>
<td> <button type="button" value="View Building" ng-click="viewBuilding()">View Building</button></td>
</tr>
</table>
</div>
app.js
var app = angular.module('myApp', []);
app.controller('MainCtrl', function($scope, $http) {
$scope.name = 'Happy Building Company';
$scope.buildings = null;
$scope.target1 = '/building_dir.json';
$scope.building = $http.get($scope.target1)
.then(
function successCall(response) {
$scope.buildings = response.data.buildings;
},
function errorCall(response) {
$scope.feedback = "Error reading building directory";
$scope.buildings = null;
}
);
//View Building - NEED TO WORK OUT HOW TO SPECIFY TO A BUILDING
$scope.viewbuilding = function(){
$scope.buildingDirVisible = false;
$scope.buildingFormVisible = true; // Building form visible
};
building_dir.json
{"buildings":[{"ID":"123",
"Owner":"Joe Doe",
"Address":"20 Lambton Quay",
"BuildingType":null,
"ConstructionDate":"0001-01-01T00:00:00"
},
{"ID":"222",
"Owner":"John Smith",
"Address":"20 Dixon Street",
"BuildingType":null,
"ConstructionDate":"0001-01-01T00:00:00"
}]}
我试图拥有它,以便当您在建筑目录中的表中选择查看建筑时,buildingDirVisible div 被隐藏并打开另一个 div,显示仅与该建筑 ID 相关的信息(即,建筑 ID 222 的建筑类型和建设日期)。目前,我的应用程序使用 show 和 hide 在 div 之间切换,但不显示数据特定的详细信息。我的理解是,我必须开发一个 for 循环来遍历 JSON 文件,然后显示与建筑物 ID 匹配的建筑物。我对 angular js 比较陌生,所以任何帮助都将不胜感激!谢谢!!
【问题讨论】: