gpt4 book ai didi

json - AngularJs:基于模型添加类

转载 作者:行者123 更新时间:2023-12-03 23:20:04 24 4
gpt4 key购买 nike

如果事情属实,只是尝试申请一个类(class)。文档非常简短 http://docs.angularjs.org/api/ng.directive:ngClass

试图添加一个类 favoriteli如果 1 === true
这是我的模拟对象

    $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
}}
];

这是我的标记。
        <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>

添加了 jsFiddle 以提高可读性,由于缺少依赖项(requireJs 等)而实际上不起作用

http://jsfiddle.net/HtJDy/

谁能指出我正确的方向? :}

最佳答案

ngClass 正在寻找一个 Angular 表达式来进行评估,“评估的结果可以是一个字符串,表示以空格分隔的类名、数组或类名到 bool 值的映射。”

为了使您的示例正常工作,它与您的想法有些相反,左侧是您要添加的类,右侧是 bool 表达式。

<li ng-repeat="restaurant in restaurantListFromSearch" ng-class="{ favorite : restaurant.details.userFavorite == 1}">

如果您愿意,这样的对象映射允许您拥有多个类:
<li ng-repeat="restaurant in restaurantListFromSearch" ng-class="{ favorite : restaurant.details.userFavorite == 1, otherClass: otherBooleanExpression }">

另请注意, bool 表达式并不完全是 JavaScript……如果插入严格的等于 '===',则会出现错误。

希望有帮助!

关于json - AngularJs:基于模型添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13923848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com