What is the reason for AngularJS rendering boxes in a new row during loop iterations

I have a collection of items that I would like to showcase next to each other in rows.

The static HTML code I have currently works perfectly.

<div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>

However, when attempting to achieve the same layout using an AngularJS loop, I encountered some issues.

<div ng-repeat="product in Products">
    <div class="box">
        // additional content will be inserted here.
    </div>
</div>

The result I obtained was not as expected.

This is my current CSS class styling:

.box {
padding : 5px;
display : inline-block;
min-width: 100px;
min-height: 50px;
background-color: red;
}

What modifications should I make to ensure the items are displayed side by side and move onto the next row if the screen width is full?

Answer №1

SOLVING ORIGINAL ISSUE

The ng-repeat directive should be placed on the

<div class="box">
.

<div>
    <div ng-repeat="product in Products" class="box">
        //details will be filled here.
    </div>
</div>

EXPLANATION OF MISTAKE

By placing the code as you did, a new

<div class="box">
is generated for each item in Products.

Your previous method resulted in creating multiple container elements for each product in Products.

An understandable mistake to make.

CORRECTING CSS STYLING

To achieve the desired style seen in your original post, you should add margin to the repeated elements for spacing between them. Modify as follows:

.box {
    padding : 5px;
    display : inline-block;
    min-width: 100px;
    min-height: 50px;
    background-color: red;
}

to

.box {
    margin-right : 5px;
    display : inline-block;
    min-width: 100px;
    min-height: 50px;
    background-color: red;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Comparing two arrays of objects and updating the first object when a matching value is found in the second object: A guide

Trying to update the first array based on matching data from the second array. Encounter an error with forEach loop. First array: const data = [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }, { id: 3, name: "Charlie" } ] S ...

jQuery Hide/Show Not Working as Expected

I am currently developing a Single Page Application using jQuery along with Semantic and Bootstrap for the UI. I have encountered an issue where jQuery is struggling to hide and show two elements on the same level, even though it works fine elsewhere in th ...

Enhance Your Forms with Bootstrap 4 Input Extension

I've been struggling to create a form with Bootstrap 4, specifically setting the value in an input text client-side for a modal form. No matter what I try, it just doesn't seem to work... Here is the form: $('#contact-modal').on(& ...

The Lifecycle of an HTML Page

Trying to grasp the life cycle of an HTML page has been a challenge for me. Unable to find comprehensive resources online, I took matters into my own hands by experimenting with the f12 tool in Internet Explorer. Through these experiments, I have formulate ...

Seeking a more streamlined approach to coding in Javascript

Is there a more efficient way for me to implement the toggleFullscreen() function? Currently, I am redundantly setting the style rules for each browser, which seems unnecessary. function toggleFullScreen() { var elem = document.getElementById("vid ...

Utilize the technique on the designated variable

I need to modify a variable to use a method in the following way; var method = ".wrap"; jQuery('.container')+[method]+("<div>Hello World</div>"); The desired outcome is; jQuery('.container').wrap("<div>Hello Worl ...

Tips for placing <div .right> above <div .left> when the window is small, given that <div .right> is positioned to the right of <div .left> when the window is large

I've come across a similar layout before, but I'm struggling to replicate it myself. The idea is to have text aligned on the left side with an image floated to the right. Instead of the image appearing below the text when the window size is red ...

"Encountering an unexpected validation error while using the LoginForm in Django

The Login and Register Model form I created is experiencing some issues. The Register form works well, but when I try to submit the Login form, it displays a validation error: "User with this Email already exists" This error message was not defined by ...

rearrange the div elements by shifting each one into the one preceding it

Do you consider this a child and parent div relationship? <div class="container"> <p>content</p> </div> <div class="footer"><p>content</p></div> <div class="container"> <p>content</p> < ...

What is the best way to create an animation for a dynamic menu background image

While hovering over a list item, I want to create an animation where the background image appears to zoom out. $(function () { var image = $('.menu').find('img').attr('src'); $('.menu ul li').mouseover(fun ...

Tips for sending a JavaScript parameter to PHP

I am implementing a pop-up modal window that retrieves data from the myForm and saves the email field value to a JavaScript variable in index.php. How can I pass this JavaScript value to PHP and display it using echo, without refreshing the index.php windo ...

Validating radio buttons in Ionic framework

I am currently working on implementing a form in my application using Ionic and AngularJS. I have added validation to all the fields, but I am facing an issue with the radio button validation. The error message is being displayed correctly when no radio bu ...

JQuery UI button failing to reset correctly after making an AJAX request

One of the features on my website is a form that allows users to update their first and last names. When they click an "Edit" button, a dialog box appears on the screen with input fields for them to make changes. <div id="nameChange"> <table widt ...

Understanding the syntax of a JSON array in jQuery or JavaScript

Hi there, I'm trying to understand this array: { "1":{ "name":"41.52862795108241,-5.397956371307373", "location":"41.52862795108241,-5.397956371307373", "banner":"http:\/\/wpwebsiteinaweekend.com\/wp-content&bs ...

Concealing elements such as buttons, icons, and text dynamically with AngularJS depending on screen resolution

I have mastered responsive html using Bootstrap, but now I need to dynamically remove elements based on screen resolution. Let's compare browser view versus mobile view: https://i.sstatic.net/q53aa.jpg Depending on the resolution, certain buttons s ...

A tutorial on making a POST request using axios in a React application

I am struggling with my JavaScript skills I need assistance with calling the following CURL command successfully: curl -X POST -H "Content-Type: application/json" -u john.doe:moqui -d "{\"firstName\":\"Hung&bso ...

Function in Javascript that can pull data for multiple users

I am facing an issue with my function that retrieves my friends' ids. It works for all friend ids but I can't seem to make it work for just one friend's id. I believe I need to implement a loop to fetch all friend ids. Could you provide ass ...

Can a vertical line be sketched next to a horizontal line?

Let's keep it short and sweet. Here's what I'm attempting to achieve: https://i.sstatic.net/e2ZU2.png Currently, this is what I have: https://i.sstatic.net/cagv7.png Below is the code snippet: #ligne_horizontale_experience { ma ...

Here is a unique version of the text: "Implementing a JavaScript function in Angular to activate the data-bs

Attempting to use JavaScript in angular to close/hide a bootstrap5 modal, encountering an issue where the hide function in bootstrap is not working. When manually clicking a button with data-bs-dismiss attribute, it closes the modal as expected, but when t ...

The functionality of Protovis JavaScript is not supported within a dropdownlist's onchange event

I encountered an issue where one block of code works fine on its own, but when combined with another block, only one of them functions properly. The problem arises when I try to invoke a method from a dropdownlist using the onchange event, especially afte ...