html - 在 div 中使用 ng-repeat 调整外部 div 的位置

标签 html css angularjs alignment

我正在使用 AngularJS v1.4.3。 Here is a fiddle of my code so far .

我正在使用 JSON 数据制作一个简单的过滤系统,其输出会根据用户选择的类别而变化。

我正在使用 ng-repeat显示 JSON 对象中的数组元素。每个对象都有一个 attrs数组属性。但是,一些 attrs数组有两个元素,有些数组有三个。

这是 HTML 代码的相关部分:

<div class="cards">
    <div ng-repeat="card in cards | filter:itemFilter" class="card_background">
        <div class="card_title">{{card.name | uppercase}}</div> 
        <div class="card_properties">
                <div ng-repeat="attr in card.attrs">{{attr}}</div>
        </div>
    </div>
</div>

以下是相关的 CSS 类:

.cards {
    display: inline-block;
}

.card_background {
    border-radius: 5px;
    border: 2px solid black;
    width: 130px;
    height: 150px;
    background-color: #DBC282;
    margin: 20px;
    padding: 20px;
    display: inline-block;
    color: black;
}

.card_title {
    letter-spacing: -1px;
    font-size: 140%;
    text-align: center;
}

.card_properties {
    text-transform: capitalize;
}

这是一些示例 JSON 数据:

$scope.cards = [
    { name: 'Sniper',       attrs: ['shooting', 'dribbling'] },
    { name: 'Finisher',     attrs: ['shooting', 'physical'] },
    { name: 'Deadeye',      attrs: ['shooting', 'passing'] },
    { name: 'Marksman',     attrs: ['shooting', 'dribbling', 'physical'] },
    { name: 'Hawk',         attrs: ['pace', 'shooting', 'physical'] },
    ...

如您所见,<div ng-repeat="attr in card.attrs">{{attr}}</div> 行是列出 attrs 的那个.并且因为它在某些情况下制作两个 div,而在其他情况下制作三个,所以对齐方式会因卡而异: attrs alignment issue

处理此问题的有效方法是什么?

最佳答案

vertical-align: top; 添加到您的 .card_background 选择器。

JSFiddle

angular.module('chemistry', [])
    .controller('MainCtrl', function ($scope, $http) {

    //	was trying to get json from separate file
    // $http.get('cards.json')
    // 	.then(function(result){
    // 	$scope.cards2 = result.data;
    // });

    //18 total
    $scope.cards = [{
        name: 'Sniper',
        attrs: ['shooting', 'dribbling']
    }, {
        name: 'Finisher',
        attrs: ['shooting', 'physical']
    }, {
        name: 'Deadeye',
        attrs: ['shooting', 'passing']
    }, {
        name: 'Marksman',
        attrs: ['shooting', 'dribbling', 'physical']
    }, {
        name: 'Hawk',
        attrs: ['pace', 'shooting', 'physical']
    }, {
        name: 'Artist',
        attrs: ['passing', 'dribbling']
    }, {
        name: 'Architect',
        attrs: ['passing', 'physical']
    }, {
        name: 'Powerhouse',
        attrs: ['passing', 'defending']
    }, {
        name: 'Maestro',
        attrs: ['shooting', 'passing', 'dribbling']
    }, {
        name: 'Engine',
        attrs: ['pace', 'passing', 'dribbling']
    }, {
        name: 'Sentinel',
        attrs: ['defending', 'physical']
    }, {
        name: 'Guardian',
        attrs: ['dribbling', 'defending']
    }, {
        name: 'Gladiator',
        attrs: ['shooting', 'defending']
    }, {
        name: 'Backbone',
        attrs: ['passing', 'defending', 'physical']
    }, {
        name: 'Anchor',
        attrs: ['pace', 'defending', 'physical']
    }, {
        name: 'Hunter',
        attrs: ['pace', 'shooting']
    }, {
        name: 'Catalyst',
        attrs: ['pace', 'passing']
    }, {
        name: 'Shadow',
        attrs: ['pace', 'defending']
    }];

    $scope.options = [{
        name: 'pace',
        selected: false
    }, {
        name: 'shooting',
        selected: false
    }, {
        name: 'passing',
        selected: false
    }, {
        name: 'dribbling',
        selected: false
    }, {
        name: 'defending',
        selected: false
    }, {
        name: 'physical',
        selected: false
    }];

    $scope.itemFilter = function (item) {
        var filters = $scope.options.filter(function (element, idx, array) {
            return element.selected;
        }) || [];

        var matched = true;
        filters.forEach(function (option) {
            matched = matched && item.attrs.indexOf(option.name) >= 0;
        })
        return matched;
    };
});
@import url(http://fonts.googleapis.com/css?family=Exo:200);
 @import url(http://fonts.googleapis.com/css?family=Exo:500);
 @import url(http://fonts.googleapis.com/css?family=Exo:300);
 @import url(http://fonts.googleapis.com/css?family=Exo:700);
 body {
    background-image: url('fifa_background_image.jpg');
    background-color: black;
    background-attachment: fixed;
    color: white;
    font-family:'Exo', sans-serif;
    font-weight: 200;
}
.main_title {
    margin: auto;
    width: 50%;
    padding: 10px;
    /*TEXT SETTINGS*/
    letter-spacing: -1px;
    color: #ffcc00;
    text-align: center;
    font-family:'Exo', sans-serif;
    font-weight: 500;
    font-size: 300%;
}
.subtitle {
    color: #dcdcdc;
    text-align:center;
}
.options_container {
    margin: auto;
    padding: 5px;
}
.option {
    display: inline-block;
    /*TEXT SETTINGS*/
    color: #dcdcdc;
    /*light grey*/
    font-weight: 300;
    font-size: 200%;
    padding: 10px;
    /*Prevent div being highlighted upon click*/
    -webkit-user-select: none;
}
.option:hover {
    -webkit-transition: 0.3s;
    color: white;
}
/*Colour the category text*/
 :checked + span {
    -webkit-transition: 0.1s;
    color: #ffcc00;
    text-shadow: 0px 0px 30px gray;
}
.check {
    /*This element is hidden */
    width: 0%;
    visibility: hidden;
}
.cards {
    display: inline-block;
}
.card_background {
    border-radius: 5px;
    border: 2px solid black;
    width: 130px;
    height: 150px;
    background-color: #DBC282;
    margin: 20px;
    padding: 20px;
    display: inline-block;
    /*TEXT SETTINGS*/
    color: black;
    vertical-align: top;
}
.card_title {
    letter-spacing: -1px;
    font-size: 140%;
    text-align: center;
}
.card_properties {
    text-transform: capitalize;
}
<title>Chemistry Cards</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
<body ng-controller="MainCtrl" ng-app="chemistry">
    <div class="main_title">CHEMISTRY STYLES FINDER</div>
    <div class="subtitle">Select categories to filter cards</div>
    <div class="options_container">
        <label ng-repeat="option in options" class="option">
            <input type="checkbox" class="check" ng-model="option.selected" />	<span>{{option.name | uppercase}}</span>

        </label>
    </div>
    <div class="cards">
        <div ng-repeat="card in cards | filter:itemFilter" class="card_background">
            <div class="card_title">{{card.name | uppercase}}</div>
            <div class="card_properties">
                <div ng-repeat="attr in card.attrs">{{attr}}</div>
            </div>
        </div>
    </div>
    <script src="ang.js"></script>
    <script src="animations.js"></script>
</body>

</html>

关于html - 在 div 中使用 ng-repeat 调整外部 div 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31594722/

相关文章:

html - 提交后旧的谷歌表单重定向

css - 带模态的导航栏

javascript - 为什么我的菜单没有下拉菜单?

带有angularjs的CSS幻灯片动画

javascript - 为什么以下函数输入不适用于给定的 AngularJS 循环?

javascript - Angular 应用程序因一行代码而损坏,仅适用于 Safari,可在所有其他浏览器中工作

html - 增加 Flexbox 容器高度而不导致行内容溢出

html - css - 无法让下拉导航显示其下方的上面内容

html - 如何制作 Skype 友好链接(预览图像标签)

CSS:在我的下拉菜单顶部放置一个带边框的箭头/三 Angular 形