我使用$ http.get構建我的第一個Angular應用程序,以便從github中提取原始json文件,並將每個json文件的輸出在頭文件在HTML中。我有的代碼正在工作,但非常多餘,我希望它可以重構。
我的控制器看起來是這樣的:
.controller('mycontroller', function($scope, $http) {
var firstUrl ='https://raw.githubusercontent.com/user/repo/master/first.json';
var secondUrl ='https://raw.githubusercontent.com/user/repo/master/second.json';
var thirdUrl ='https://raw.githubusercontent.com/user/repo/master/third.json';
var fourthUrl ='https://raw.githubusercontent.com/user/repo/master/fourth.json';
var fifthUrl ='https://raw.githubusercontent.com/user/repo/master/fifth.json';
var sixthUrl ='https://raw.githubusercontent.com/user/repo/master/sixth.json';
$http.get(firstUrl).success(function(data) {
$scope.firsts = data;
});
$http.get(secondUrl).success(function(data) {
$scope.seconds = data;
});
$http.get(thirdUrl).success(function(data) {
$scope.thirds = data;
});
$http.get(fourthUrl).success(function(data) {
$scope.fourths = data;
});
$http.get(fifthUrl).success(function(data) {
$scope.fifths = data;
});
$http.get(sixthUrl).success(function(data) {
$scope.sixths = data;
});
});
我的HTML看起來像這樣:
<div ng-controller="mycontroller" id="content" class="container">
<br/>
<div ng-repeat="first in firsts" class="panel panel-default">
<h1 id="first">first</h1>
<div class="panel-heading">
<h1><a href="{{first.url}}">{{first.company}}</a></h1>
<p>{{first.address}}</p>
</div>
<div class="list-group">
<a ng-repeat="position in first.positions" class="list-group-item" href="{{first.url}}">
{{position.title}}
</a>
</div>
</div>
</div>
<div ng-controller="mycontroller" id="content" class="container">
<h1 id="second">second</h1>
<hr/>
<div ng-repeat="second in seconds" class="panel panel-default">
<div class="panel-heading">
<h1><a href="{{second.url}}">{{second.company}}</a></h1>
<p>{{second.address}}</p>
</div>
<div class="list-group">
<a ng-repeat="position in second.positions" class="list-group-item" href="{{second.url}}">
{{position.title}}
</a>
</div>
</div>
</div>
<div ng-controller="mycontroller" id="content" class="container">
<h1 id="thirds">third</h1>
<hr/>
<div ng-repeat="third in thirds" class="panel panel-default">
<div class="panel-heading">
<h1><a href="{{third.url}}">{{third.company}}</a></h1>
<p>{{third.address}}</p>
</div>
<div class="list-group">
<a ng-repeat="position in third.positions" class="list-group-item" href="{{third.url}}">
{{position.title}}
</a>
</div>
</div>
</div>
<div ng-controller="mycontroller" id="content" class="container">
<h1 id="fourth">fourth</h1>
<hr/>
<div ng-repeat="fourth in fourths" class="panel panel-default">
<div class="panel-heading">
<h1><a href="{{fourth.url}}">{{fourth.company}}</a></h1>
<p>{{fourth.address}}</p>
</div>
<div class="list-group">
<a ng-repeat="position in fourth.positions" class="list-group-item" href="{{fourth.url}}">
{{position.title}}
</a>
</div>
</div>
</div>
<div ng-controller="mycontroller" id="content" class="container">
<h1 id="fifth">fifth</h1>
<hr/>
<div ng-repeat="fifth in fifths" class="panel panel-default">
<div class="panel-heading">
<h1><a href="{{fifth.url}}">{{fifth.company}}</a></h1>
<p>{{fifth.address}}</p>
</div>
<div class="list-group">
<a ng-repeat="position in fifth.positions" class="list-group-item" href="{{fifth.url}}">
{{position.title}}
</a>
</div>
</div>
</div>
<div ng-controller="mycontroller" id="content" class="container">
<h1 id="sixth">sixth</h1>
<hr/>
<div ng-repeat="sixth in fifths" class="panel panel-default">
<div class="panel-heading">
<h1><a href="{{sixth.url}}">{{fifth.company}}</a></h1>
<p>{{sixth.address}}</p>
</div>
<div class="list-group">
<a ng-repeat="position in sixth.positions" class="list-group-item" href="{{fifth.url}}">
{{position.title}}
</a>
</div>
</div>
</div>
我們有幾乎相同的控制器代碼=),但我仍然認爲控制器應儘可能薄,打破標記可重複使用的餡餅ces(指令)和從這些片段組成index.html好得多。 – Denis
我喜歡這種方法。唯一的問題是'$ q.all'是**不會失敗彈性**。如果任何'$ http.get'失敗,整個列表將無法顯示。隨着每個承諾的完成,填充'data'數組將很好,因此用戶可以看到部分結果。 – georgeawg