Using Angular 1's ng-repeat along with a filter to separate string values within a single JSON array

With the use of ng-repeat, I was able to append the values from a JSON array. However, I encountered an issue with the pictureURLS object containing string values of images. My goal is to separate the string values in pictureURLS and display the images individually.

Javascript

 var results= [
                {
                   "title": Album1,
                   "pictureURLS": "pirul.jpg,picurl2.jpg,picurl3.jpg",
                   "ApprovalStatus": "2",


                },
                {
                   "title": Album2,
                   "pictureURLS": "pirul.jpg,picurl2.jpg,picurl3.jpg",
                   "ApprovalStatus": "2",


                },
                {
                   "title": Album3,
                   "AlbumPictureURLS": null,
"ApprovalStatus": "2",

             ]

HTML

<div ng-repeat="photo in results">
<h4>{{photo.title}}</h4>
<img src="{{photo.pictureURLS}}" />
</div>

Answer №1

Give this method a shot:

<img ng-repeat="link in website.imageLinks.split(',')" src="{{link}}" />

Answer №2

Give this a shot:

 <div ng-repeat="image in collection">
       <h3>{{image.heading}}</h3>
         <div ng-repeat="link in image.urls.split(',')">
            <img ng-src="{{link}}" />
         </div>

    </div>

Answer №3

Here is the updated JSON structure:

var albums= 
           [{
               name: "Music Album",
               imageUrls: "image1.jpg,image2.jpg,image3.jpg",
               status: "approved"
            },
            {
               name: "Photo Album",
               imageUrls: "photo1.jpg,photo2.jpg,photo3.jpg",
               status: "pending"
            },
            {
               name: "Vacation Album",
               imageUrls: null
            }];

Check out the code on fiddle:  http://jsfiddle.net/Abc123/5678/

Answer №4

Try implementing the following code snippet:

HTML:

<div ng-repeat="image in images">
    <h4>{{image.title}}</h4>
    <img ng-repeat="url in image.imageURLs | filterImages" src="{{url}}" />
</div>

Js:

myApp.filter('filterImages', function() {
    return function(data) {
        return data.split(',');
    };
});

Give it a try and see if it works for you.

Answer №5

If you want to customize your objects, consider storing URLs as an array like this:

for(var j=0; j<items.length; j++){
    var urlArray = items[j].urls.split(',');
    items[j].urlArray = urlArray;
}

After that, you can utilize another ng-repeat in your HTML code:

<div ng-repeat="item in items">
<h3>{{item.name}}</h3>
<div ng-repeat="link in item.urlArray">
    <a href="{{link}}">Link</a>
</div>
</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

Combine the values of two fields in real-time on a Model-View-Controller Edit form

One challenge I am facing is within an MVC Edit form that contains two fields named ExVAT and VAT. I would like to introduce a new field that displays the total of these two values. Currently, the total only reflects the sum of the two fields upon initial ...

Laravel's blade allows you to easily convert an HTML element to an array using the same name

I have two separate divs with similar content but different values <div id="tracks-1"> <div> <label>Song Title</label> <input type="text" name="tracks[song_title]" value=""> <input type="text ...

Vue encounters an issue when trying to access a specific field within an array of objects

Consider the following data structure: rules:[ 0:{ subrule1:'', subrule2:'', subrule3:'' }, 1:{ subrule1:'', subrule2:'', subrule3:'' } ...

Using Three.js to construct a tangent plane at a specific point on a sphere

I am currently working on constructing a tangent plane on the surface of a sphere at a specific point using Three.js (v0.129). The algorithm I have been following is outlined as follows: Locate the center of the sphere; Determine the radius vector that in ...

Using Node.js, Express, and Socket.IO to establish real-time communication between two static client pages

I'm in the process of creating a remote control for a gallery using nodejs, express, and socket.io. Here's the project structure: /index.js /public/screen.html /screen.js /remote.html /remote.js The goal is to display a gallery of ima ...

Divide text to reduce its width within the confines of a specific height on a div element

I've spent the past week scouring various forums, including stackoverflow, but I haven't been able to find a solution. In my responsive website, I'm using CSS flexbox to display dynamic menu items. Sometimes the text can be quite long, and ...

Exploring MapQuest API: Unraveling the process of dissecting MapQuest

I am currently exploring MapQuest navigation and utilizing JavaScript code to retrieve the data. Although I am able to extract JSON content in my application, I am unsure of how to utilize this data for navigation. I have started a new project and execute ...

Retrieve the item that is contained within a separate item within the original

Is there a way to access the Photos object in my code? I attempted to use {{ item.image.Photos[0].image }} but it was unsuccessful https://i.stack.imgur.com/IGC2c.png Looking for Assistance with TypeScript listHotelPhotos( hotel_id ){ let loader = t ...

Implementing a Context Menu with a Single Click

I need assistance with creating a context menu that appears when the user clicks inside an HTML5 canvas. I want to have functions called when an item in the menu is selected. Can anyone provide guidance on how to achieve this? ...

Using React to simulate API calls outside of testing environments

For instance, I encounter issues when certain endpoints are inaccessible or causing errors, but I still need to continue developing. An example scenario is with a function like UserService.getUsers where I want to use fake data that I can define myself. I ...

Adjust the default value of the ng-model for an input field with a date picker

One of my inputs has a datepicker attached to it. <input id="start-date" style="width: 230px;" close-text="Close" type="text" ng-model="startDate" datepicker-popup="dd-MM-yyyy" ng-required="true"/></div> When the input is displayed, I nee ...

How can I efficiently create a suffix using this JavaScript code?

Take note that the code displayed below showcases the array in the console, rather than in the snippet output var names = ["maria", "mary", "marks", "michael"]; function add_suffix(names) { var suffixes = []; for (var i = 0; i < names.length; i+ ...

The jQuery plugin fails to display properly when used in conjunction with AngularJS

Currently, I am working on a tabs application and have implemented an angularJS directive to utilize the jquery tabs plugin. Unfortunately, I am facing issues with the view not updating properly and the tabs not being displayed as expected. Here is a link ...

Learn the process of invoking an Angular scope function from within a JavaScript function located within a controller

In my Angular controller, I have a JavaScript function where I am trying to call an Angular function. However, I am encountering the error: $scope.Name is not a function, $scope.dates is not a function. function validation() { $scope.page ...

Error encountered in a pre-existing project due to Vuetify configurations

Having trouble downloading Vuetify 3 onto my Vue 3 app, I keep encountering an error. Despite attempting to use 'npm install --save @popperjs/core vuetify/styles', it's still not working as expected. I'm curious to understand the root c ...

"Utilizing multiple optional parameters in Expressjs can lead to the request handler being

Hey there, I could use a fresh perspective on this issue I'm having. I am attempting to set up a request handler that can accept 0, 1, or 2 parameters like http://localhost:3000/{seed}/{size}. The parameters "seed" and "size" should both be optional. ...

Is there a similar alternative to {useLocation} provided by 'react-router-dom' that can be used in Next.js?

import { useLocation } from 'react-router-dom'; Currently utilizing Nextjs, I'm seeking assistance in finding an alternative for useLocation ` import Link from 'next/link'; import { FC } from 'react'; import {useRout ...

React 16 is able to efficiently render an array of numbers into table data using the map function

In my quest to display the most frequently used words, I have successfully fetched data from an API. I have set up the state for words and mapped the array in render(). However, instead of displaying the words along with their counts, I only see numbers ...

Sharing data between different JavaScript files using Knockout.js

I am working with two JavaScript files named FileA.js and FileB.js. Within FileA.js, there is a function called Reports.CompanySearch.InitGrid: function InitGrid(viewModel, emptyInit) { var columns = [ { name: 'CompanyName', ...

Random sequencing of the commands

Whenever I call the Details function, it returns empty details because the function executes before retrieving data from the json file. What is the best way to fix this issue? app.controller('loginCtrl',function($scope,login){ $scope.user=login ...