只有當它的某個屬性等於數組時,我才需要顯示對象。Equal Arrays AngularJS Pro
我在app.js控制器:
app.controller('checkBoxController', function ($scope) {
$scope.ingredients= [
{label: 'Egg', value: 1},
{label: 'Milk', value: 2},
$scope.selection=[];
$scope.toggleSelection = function toggleSelection(ingredientLabel) {
var idx = $scope.selection.indexOf(ingredientLabel);
if (idx > -1) {
$scope.selection.splice(idx, 1);
}
else {
$scope.selection.push(ingredientLabel);
}
};
});
,併爲它的HTML代碼:
<span style="color:black;" class="selected-item">Selected Items:<span>
<div ng-repeat="label in selection" class="selected-item">
</div>
<div class="list-group">
<div class="list-group-item" ng-repeat="product in meals.products" ng-show="product.contents==selection">
<h1>{{product.name}}</h1>
<meal-gallery></meal-gallery>
<meal-tabs></meal-tabs>
</div>
</div>
和我的產品陣列有{ name: 'Scrambled Egg', contents: "Egg"}
。所以我需要展示產品,如果它的內容等於選定的成分。 我沒有問題,只有一個成分像「蛋」,但如果我需要兩個等於選定的內容?
angularjs pro?是某種模塊?看起來你在發佈時在你的代碼中實現了一個錯字,你的數組並不完整。 – 2015-04-01 20:35:27
除非兩個對象字面上是相同的對象,否則不能以這種方式使用indexOf。 'var x = {}; var y = x; x == y; // true''var x = {'name':'pie'}; x == {'name':'pie'}; // false' – 2015-04-01 20:38:30