Chỉ cố gắng áp dụng một lớp nếu có điều gì đó đúng. Các tài liệu là rất ngắn gọn về http://docs.angularjs.org/api/ng.directive:ngClassAngularJs: Thêm lớp dựa trên Mẫu
Cố gắng thêm một lớp của favorite
trên li
nếu 1 === true
Dưới đây là Object Mock tôi
$scope.restaurantListFromSearch = [
{restaurantName: "Zocala",
details: {
image: "http://placehold.it/124x78",
url: "#",
cuisineType: ["Sandwiches", "American", "BBQ"],
description: "Modern, healthy, awesome BBW",
priceRange: "$",
userFavorite: 0
}},
{restaurantName: "Subway",
details: {
image: "http://placehold.it/124x78",
url: "#",
cuisineType: ["Sandwiches", "American", "BBQ"],
description: "Modern, healthy, awesome BBW",
priceRange: "$",
userFavorite: 1
}},
{restaurantName: "Hungry Howies",
details: {
image: "http://placehold.it/124x78",
url: "#",
cuisineType: ["Sandwiches", "American", "BBQ"],
description: "Modern, healthy, awesome BBW",
priceRange: "$",
userFavorite: 0
}}
];
Và đây là đánh dấu của tôi.
<ul>
<li ng-repeat="restaurant in restaurantListFromSearch" ng-class="{restaurant.details.userFavorite === 1: favorite}">
<img src="{{restaurant.details.image}}" alt="{{restaurant.restaurantName}}">
<div class="details">
<a href="{{restaurant.details.url}}">{{restaurant.restaurantName}}</a>
<span class="cuisine-type">{{restaurant.details.cuisineType}}</span>
<p>{{restaurant.details.description}}</p>
<span class="price-rating">{{restaurant.details.priceRange}}</span>
</div>
<div class="clear"></div>
</li><!-- end restaurant result -->
</ul>
Added jsFiddle cho dễ đọc, không thực sự làm việc do thiếu phụ thuộc (requireJs, vv)
bất cứ ai có thể chỉ cho tôi đi đúng hướng? :}
Awe, tôi đã rất gần! Điều này hoạt động hoàn hảo. Cảm ơn nhiều! –
cho các lớp được phân tách bằng dấu "-", đặt tên lớp trong dấu ngoặc kép như 'tên lớp' để tránh lỗi cú pháp – imsheth