Showing elements of an array with personalized spacing in angularjs

Looking to show a string[], here's the setup:

JS Code

var result = ["Fish","Mutton","Shrimp","Chicken"];
$scope.res = result;

All array values are retrieved in $scope.res and displayed in HTML using ng-bind,

HTML code

<span ng-bind="res"></span>

Result: Fish,Mutton,Shrimp,Chicken

Desired Result: Fish, Mutton, Shrimp, Chicken

Adding spaces between each value is needed.

Answer №1

To achieve this, you can utilize the join method.

Utilizing the join() method allows you to combine all elements of an array (or a similar object) into a single string.

JavaScript

var finalResult = ["Apple","Banana","Orange","Grapes"];
$scope.output = finalResult.join(', ');

HTML

<span ng-bind="output"></span>

var myApp = angular.module('myApp', []);
myApp.controller('CtrlOne', function ($scope) {
    var finalResult = ["Apple","Banana","Orange","Grapes"];
    $scope.output = finalResult.join(', ');
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
    <fieldset ng-controller="CtrlOne">
        <span ng-bind="output"></span>
    </fieldset>
</div>

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

Expression fragment in Thymeleaf

In splitting my templates into head/main/footer parts using thymeleaf, I have found a method to include stylesheets and javascript on certain pages while excluding them from others. This is achieved through the use of fragment expressions outlined here. M ...

python Efficiently load zipfile data into a numpy array

I am searching for a solution to efficiently read a zipfile into memory and extract its contents into a numpy array with numpy datatypes. The challenge lies in the fact that these files are large in size and there are numerous of them, making speed a cruci ...

Is there a way to determine completion of page loading in an AngularJS application using JavaScript?

I am currently in the process of crafting Robot Framework tests to address specific use cases for an external AngularJS application. One of my requirements is the utilization of Python 3.5+ and SeleniumLibrary instead of the outdated Selenium2Library. In ...

Implement a code to apply to an image that is loaded dynamically

I have a situation on a page where an image is loaded via ajax within a wrapping div. I need to execute some code as soon as that image is loaded. Unfortunately, I am unable to modify the ajax call, which means using on('success') directly on the ...

The navigation bar in javascript automatically removes the active class once the page has finished loading

I am having an issue with my code where the active class is being removed after a page refresh. Below is the HTML code: <div id="navbar" class="navbar-collapse collapse navbar-right"> <ul class="nav navbar-nav> <li><a href="abo ...

Nuxt.js is throwing a TypeError because it is unable to access the 'minify' property since it is undefined

When I try to generate a Nuxt app using "npm run generate" or "npm run build", I encounter an issue where it throws a TypeError: Cannot read property 'minify' of undefined. Does anyone know how to solve this? TypeError: Cannot read property &apo ...

Issue in d3.js: bisector consistently returning zero

http://jsfiddle.net/rdpt5e30/1/ const data = [ {'year': 2005, 'value': 771900}, {'year': 2006, 'value': 771500}, {'year': 2007, 'value': 770500}, {'year': 2008, 'value&apos ...

Implement seamless content loading using jQuery, eliminating the need

Is there a reason why this piece of code isn't working for me? I'm trying to load HTML content using jQuery and followed the instructions from this tutorial: Here's how my HTML looks: <div id="icon"> <a href="http://localhost/ ...

How can you determine the number of distinct arrays within an array using PHP?

I'm working with an array that contains multiple arrays: Array ( [0] => Slip Object ( [userId:protected] => 1 [parentSlipId:protected] => 0 [id:protected] => 25 [madeDatetime:pro ...

Automating the process of running npm start on page load: A guide

Recently, I've been delving into learning npm in order to incorporate it into a website. I'm curious about how exactly it is used within a website - do you typically need to execute the command "npm start"? How does this integration work for a li ...

Accessing attributes declared in the constructor from within class methods is not possible

I am encountering an issue with my HomeController and its index method. I have declared a variable called `this.data` in the constructor, but when I try to access it within the index method, I get the following error message: TypeError: Cannot read proper ...

Exploring Angular's ng-transclude directive within a repeat loop

Recently, I began delving into AngularJS and attempted to create a custom table directive with multiple slots for transclusion. However, I encountered an issue where the scope was not being passed to the transclude. Although there are various solutions ava ...

Ways to designate a parent element in Vue Draggable when the element is lacking a child

I'm currently incorporating vue-draggable into my project from the following GitHub repository: https://github.com/SortableJS/Vue.Draggable Below is my ElementsList component: <div> <draggable v-model="newElement" :move ...

During multiple-array references, what action is the dereference operator instructing the compiler to take?

Recently, I've been experimenting with multidimensional arrays and bracket notation in an attempt to grasp the relationship between dereferencing, pointer type, and pointer arithmetic. Let's focus on a hypothetical 3D array reference for this di ...

What is the method to execute the onclick command once a selection has been made from the drop-down menu?

Whenever I click on the dropdown menu, the function reload is triggered immediately. How can I make the reload function run only after selecting an item from the dropdown menu? Any assistance would be greatly appreciated. <form><select name="st ...

Tips for transferring the data from one yform value to another field

Within our online store, some products feature a yForm to consolidate various parts of the product. Is there a straightforward method to automatically transfer the sum field value to another field, such as the product quantity (which does not use yForm)? I ...

"Troubleshooting the lack of functionality in nested ajax calls, click events, or event

Initially, a navigation click event was created: $('#inner-navigation li a') .on('click', function (e) { e.preventDefault(); AjaxNavUrl.checkURL(this.hash); }); This event triggers an ajax call and respo ...

Display sub navigation when clicked in WordPress

I currently have the default wordpress menu setup to display sub navigation links on hover, but I am interested in changing it so that the sub navigation only appears when the user clicks on the parent link. You can view my menu here https://jsfiddle.net/f ...

"Exploring the benefits of using nested mapping for res.json() in an Express application

I have been developing an express application (server-side) that offers movie information to users, and I am attempting to send a JSON response in the following format: { "title": "Star Trek: First Contact", "year": 1996, ...

Pre-submission validation in JavaScript for AJAX requests

I have encountered two issues while trying to validate a form before initiating an ajax submission. The first issue is determining the most professional process for validating the form before submission. The second issue involves identifying what is preven ...