gpt4 book ai didi

javascript - AngularJS:ng-repeat 跨多个 div

转载 作者:搜寻专家 更新时间:2023-10-31 22:53:56 26 4
gpt4 key购买 nike

我希望我网站的一部分是图像和信息的拼贴画,如下所示:

My aim

我可以将其作为一系列 div 手动执行:

<div class="options">
<div class="option">
<a href="https://www.handpickedhotels.co.uk/bailbrookhouse"><img class="opt_img" src="images/hotels/BBH.jpg"></a>
<h3 class="centered" id="hotel_info">Bailbrook House Hotel<br>BA1 7JD<br>Approx Price: &pound;xx</h3>
</div>
<div class="option">
<a href="https://www.travelodge.co.uk/hotels/75/Bath-Central-hotel"><img class="opt_img" src="images/hotels/bath_central_travelodge.jpg"></a>
<h3 class="centered" id="hotel_info">Bath Central Travelodge<br>BA1 2EB<br>Approx Price: &pound;xx </h3>
</div>
<div class="option">
<a href="https://www.travelodge.co.uk/hotels/361/Bath-Waterside-hotel"><img class="opt_img" src="images/hotels/bath_waterside_travelodge.jpg"></a>
<h3 class="centered" id="hotel_info">Bath Waterside Travelodge<br>BA2 4JP<br>Approx Price: &pound;xx</h3>
</div>
</div>
<div class="options">
<div class="option">
<a href="https://www.premierinn.com/gb/en/hotels/england/somerset/bath/bath-city-centre.html"><img class="opt_img" src="images/hotels/Bath_premier_inn.jpg"></a>
<h3 class="centered" id="hotel_info">Bath City Centre Premier Inn<br>BA1 2BX<br>Approx Price: &pound;xx</h3>
</div>
<div class="option">
<a href="https://www.yha.org.uk/hostel/yha-bath"><img class="opt_img" src="images/hotels/YHA.jpg"></a>
<h3 class="centered" id="hotel_info">YHA Bath<br>BA2 6LA <br>Approx Price: &pound;xx </h3>
</div>
<div class="option">
<a href="https://www.bailbrooklodge.co.uk/"><img class="opt_img" src="images/hotels/bailbrook_lodge.jpg"></a>
<h3 class="centered" id="hotel_info">Bailbrook Lodge<br>BA1 7HZ<br>Approx Price: &pound;xx</h3>
</div>

和以下 CSS:

*{
font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif
}

#hotel_info{
background-color: rgba(130, 130, 130,0.7);
width: 80%;
}

.centered {
position: absolute;
width: 100%;
top: 40%;
left: 50%;
transform: translate(-50%, -50%);
}

.options{
width: 100%;
overflow: hidden;
}

.option {
position: relative;
text-align: center;
width: 33%;
float:left;
}

.opt_img{
width: 100%;
opacity: 0.7;
}

我现在开始使用 AngularJS 来更轻松地显示这些信息。

我有 MainController.js

app.controller('MainController', function($scope){
$scope.title = 'Hotels';
$scope.hotels = [
{
name: 'Bailbrook House Hotel',
postcode: "BA1 7JD",
price: 0.00,
website: "https://www.handpickedhotels.co.uk/bailbrookhouse",
img: "images/hotels/BBH.jpg"
},
{
name: "Bath Central Travelodge",
postcode: "BA1 2EB",
price: 0.00,
website: "https://www.travelodge.co.uk/hotels/75/Bath-Central-hotel",
img: "images/hotels/bath_central_travelodge.jpg"
},
{
name: "Bath Waterside Travelodge",
postcode: "BA2 4JP",
price: 0.00,
website: "https://www.travelodge.co.uk/hotels/361/Bath-Waterside-hotel",
img: "images/hotels/bath_waterside_travelodge.jpg"
},
//... [and so on]
]
});

然后我用了

<div ng-controller="MainController">
<div class = "options" ng-repeat="hotel in hotels">
<div class = "option">
<a ng-href={{hotel.website}}><img class="opt_img" ng-src={{hotel.img}}></a>
<h3 class="centered" id="hotel_info">{{hotel.name}}<br>{{hotel.postcode | uppercase}}<br>Approx Price: {{hotel.price | currency:'&pound;'}}</h3>
</div>
</div>
</div>

这会将所有选项放在一起。我明白为什么会这样,因为它不会像我手动制作时那样围绕每组 3 创建选项 div。是否有可能通过 ng-repeat 实现这一目标,或者我是否需要重新考虑我的做法?


我是网络开发的新手,所以请让您的回答对初学者友好,如果我需要澄清,请耐心等待。我也乐于接受人们提出更好或更简单的做事方式的建议,我只是使用 Codecademy 的 AngularJS 类(class)作为起点。

最佳答案

这可以通过使用 css flex 属性轻松实现

<div ng-controller="MainController" class="hotel-container">
<div class = "options" ng-repeat="hotel in hotels">
<div class = "option">
<a ng-href={{hotel.website}}><img class="opt_img" ng-src={{hotel.img}}></a>
<h3 class="centered" id="hotel_info">{{hotel.name}}<br>{{hotel.postcode | uppercase}}<br>Approx Price: {{hotel.price | currency:'&pound;'}}</h3>
</div>
</div>
</div>

.hotel-container {
display: flex;
}
.options {
width : 33.33%;
}

在这里阅读更多弹性属性:https://css-tricks.com/almanac/properties/f/flex/

关于javascript - AngularJS:ng-repeat 跨多个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50543425/

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