我有大約有一些書細節JSON文件:角for each循環行爲
JSON文件(我已經貼只是爲了顯示參數):
{
"books": [{
"bookId":101,"bookTitle":"Angular JS","topic":"Angular JS",
"author":"Green",
"cost":375,
"imgUrl":"images/AngularJS1.JPG",
"issued":true
}, {
"bookId":102,
"bookTitle":"Instant Angular JS Starter",
"topic":"Angular JS",
"author":"Dan Menard",
"cost":150,
"imgUrl":"images/AngularJS2.JPG",
"issued":true
}, {
"bookId":103,
"bookTitle":"Ng-Book:The Complete Book on Angular JS",
"topic":"Angular JS",
"author":"Ari Lerner",
"cost":4657,
"imgUrl":"images/AngularJS3.JPG",
"issued":false
}, {
"bookId":104,
"bookTitle":"Developing BackbonJS Applications",
"topic":"Backbone JS",
"author":"Addy Osmani",
"cost":650,
"imgUrl":"images/BackboneJS1.JPG",
"issued":true
}, {
"bookId":105,
"bookTitle":"Backbone.js Patterns and Best Practices",
"topic":"Backbone JS",
"author":"Swarnendu De",
"cost":390,
"imgUrl":"images/BackboneJS2.JPG",
"issued":false
}, {
"bookId":106,
"bookTitle":"Backbone.js Cookbook",
"topic":"Backbone JS",
"author":"Vadim Mirgorod",
"cost":240,
"imgUrl":"images/BackboneJS3.JPG",
"issued":false
}, {
"bookId":107,
"bookTitle":"Ember.js in Action",
"topic":"Ember JS",
"author":"Joachim Haagen Skeie",
"cost":2500,
"imgUrl":"images/EmberJS1.JPG",
"issued":false
}, {
"bookId":108,
"bookTitle":"Mastering Ember.js",
"topic":"EmberJS",
"author":"Mitchel Kelonye",
"cost":3500,
"imgUrl":"images/EmberJS2.JPG",
"issued":false
}, {
"bookId":109,
"bookTitle":"Developing an Ember JS Edge",
"topic":"EmberJS",
"author":"Jamie White and Matthew Beale",
"cost":2000,
"imgUrl":"images/EmberJS3.JPG",
"issued":false
}, {
"bookId":110,
"bookTitle":"Node.js in Action",
"topic":"NodeJS",
"author":"Mike Cantelon and Marc Harter",
"cost":800,
"imgUrl":"images/NodeJS1.JPG",
"issued":false
}, {
"bookId":111,
"bookTitle":"Node.js the Right Way",
"topic":"NodeJS",
"author":"Jim R. Wilson",
"cost":1200,
"imgUrl":"images/NodeJS2.JPG",
"issued":false
}, {
"bookId":112,
"bookTitle":"Pro Node.js for Developers",
"topic":"NodeJS",
"author":"Colin Ihrig",
"cost":2800,
"imgUrl":"images/NodeJS3.JPG",
"issued":false
}]
}
我訪問這個JSON文件使用控制器中的Angular JS的$ http服務。
我最後在HTML中顯示細節,我想用我自己的自定義過濾器添加貨幣符號,下面是代碼。
HTML代碼:
<table>
<tr class="LoginFormDiv" ng-repeat="iterator in variable|orderBy:sort |filter:mySearch|addRupeeSymbol">
<td>{{$index+1}}</td>
<td>
<img src="{{iterator.imgUrl}}"/>
</td>
<td>
<table>
<tr>
<td>Book Id:</td><td>{{iterator.bookId}}</td>
</tr>
<tr>
<td>Book Title:</td><td>{{iterator.bookTitle}}</td>
</tr>
<tr>
<td>Book Topic:</td><td>{{iterator.topic}}</td>
</tr>
<tr>
<td>Book Author:</td><td>{{iterator.author}}</td>
</tr>
<tr>
<td>Book Cost:</td><td>{{iterator.cost}}</td>
</tr>
<tr>
<td>Issued</td><td>{{iterator.issued}}</td>
</tr>
<tr>
<td>
<button ng-disabled="iterator.issued == true" ng-click="issueBook(iterator.bookId)"> Issue </button>
<button ng-disabled="iterator.issued == false" ng-click="returnBook(iterator.bookId)"> Return </button>
</td>
</tr>
</table>
</td>
</tr>
</table>
的 「addRupeeSymbol」 是我的自定義過濾器。
自定義過濾器:
var myFilter = angular.module('myFilter', []);
myFilter.filter('addRupeeSymbol', function() {
return function(input) {
var costWithRupeeSymbol = [];
angular.forEach(input, function(value, index) {
value.cost = "Rs." + value.cost;
costWithRupeeSymbol.push(value);
});
return costWithRupeeSymbol;
}
});
當代碼被執行,我得到的輸出作爲RsRsRsRsRsRsRsRsRsRsRs375,爲什麼會這樣,我無法理解這個for循環表現在這裏。 請解釋。
我試過了你建議的方式,但我想知道爲什麼循環無法編輯只有一個R的數組元素。符號,它爲此添加了11個符號?循環運行超過12次的原因是什麼? –
@ user3077946這是因爲您對數組angular.forEach進行了更改(輸入,函數(值,索引){value.cost =「Rs。」+ value.cost; costWithRupeeSymbol.push(value);});您不能在過濾器定義中更改value.cost,因爲如果您確實角度看到該更改並再次運行摘要循環,那麼運行過濾器,更改value.cost以查看該更改並反覆運行消化循環。 。在數組過濾器中,您只需將已存在的元素推送到結果數組(costWithRupeeSymbol)而不改變它們。 –