Add pictures to an array

I am facing a challenge in pushing images into an array. I want to capture photos of various items or multiple pictures of one item, but the implementation seems tricky.

Currently, only a single image is displayed from the first line:

<img ng-show="imgURI !== undefined" ng-src="{{imgURI}}" style="text-align: center">

However, when using ng-repeat, multiple images are not being displayed as intended.

I believe my problems lie in:

  1. Identifying what is incorrect with my ng-repeat?
  2. Ensuring that the images are correctly stored within the array?

HTML

 <button class="button button-full button-assertive" ng-click="takePhoto()">
    Take Photo
    </button>
    <img ng-show="imgURI !== undefined" ng-src="{{imgURI}}" style="text-align: center">
<ion-item class="item item-assertive">Test array</ion-item>
 <div ng-repeat="x in array">
     <img ng-src="{{x.imgURI}}">
 </div> center">

Javascript

  $scope.takePhoto = function () {
              var options = {
                quality: 75,
                destinationType: Camera.DestinationType.DATA_URL,
                sourceType: Camera.PictureSourceType.CAMERA,
                allowEdit: true,
                encodingType: Camera.EncodingType.JPEG,
                targetWidth: 300,
                targetHeight: 300,
                popoverOptions: CameraPopoverOptions,
                saveToPhotoAlbum: false
            };

                $scope.array = [];

                $cordovaCamera.getPicture(options).then(function (imageData) {
                    $scope.imgURI = "data:image/jpeg;base64," + imageData;
                    $scope.imgURI = array;
                }, function (err) {
                    // An error occured. Show a message to the user
                });
            }

Answer №1

Assigning the imageUri to the array's value is what you are doing.

$scope.imgURI = ar;

To add the imgURI to the array, you should use push method:

$scope.array.push($scope.imgURI);

Answer №2

experiment with this code snippet, where an array is created outside of the takePhoto function and images are pushed into it.

$scope.imageArray = [];
$scope.takePhoto = function () {
    var options = {
        quality: 75,
        destinationType: Camera.DestinationType.DATA_URL,
        sourceType: Camera.PictureSourceType.CAMERA,
        allowEdit: true,
        encodingType: Camera.EncodingType.JPEG,
        targetWidth: 300,
        targetHeight: 300,
        popoverOptions: CameraPopoverOptions,
        saveToPhotoAlbum: false
    };



    $cordovaCamera.getPicture(options).then(function (imageData) {
        $scope.imageArray.push("data:image/jpeg;base64," + imageData);
    }, function (err) {
    // Display an error message to the user
    });
}

Your HTML markup should look like the following:

<div ng-repeat="img in imageArray">
  <img ng-show="img !== undefined" ng-src="{{img}}">
</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

What is causing the ASP.NET MVC app to continuously load scripts without end?

Recently, I encountered an intriguing issue. Here's what I have: An ASP.NET MVC app; An AngularJS app embedded within this app. Here is the layout page I am working with: @using System.Web.Optimization <!DOCTYPE html> <html> <head ...

Guide on how to synchronously update a value following an Ajax request

Having an issue fetching data from my Node (Express) backend server to React frontend. Whenever I try to access the values at specific array indexes, they come out as undefined. I am sure there is a mistake in my approach. Can someone please provide me wit ...

Troubleshooting a hybrid Angular Ng1/Ng2 application that is causing an error stating 'Provider for $scope not found'

I am currently in the process of developing a hybrid Ng1/Ng2 app utilizing NgUpgrade as part of our migration plan to transition towards Angular 2. Initially, the bootstrap appears to be functioning correctly: platformBrowserDynamic().bootstrapModule(App ...

Locate and modify a specific element within an array of objects

Currently, I am working with an array that looks like this: arr = [{id:'first',name:'John'},{id:'fifth',name:'Kat'},{id:'eitghth',name:'Isa'}]. However, I need to add a condition to the array. If ...

The error message InvalidCharacterError is displayed when the attempt to create a new element using the 'createElement' method on the 'Document' object fails. This is due to the tag name provided ('/static/media/tab1.fab25bc3.png') not being a valid name

Hey everyone! I'm new to using React and I decided to try cloning Netflix by following a tutorial on YouTube. However, I've encountered an issue with rendering an image in a functional component. The error message I'm receiving is as follow ...

Preventing errors caused by undefined array elements with a type guard

TSC throws an error that is inserted as a comment into the code. tsconfig: "noUncheckedIndexedAccess": true type Tfactors = [number, number, number, number]; export default function changeEnough(pocket: Tfactors, bill: number): boolean { cons ...

Configure the right-to-left directionality for a particular widget only

Is it possible to align the label inside a TextField component to the right, similar to "labelPlacement" on FormControlLabel? I am aware of using the RTL library with mui-theme, but that applies to the entire application. Is there a way to apply it to jus ...

Leveraging jQuery plugins within an AngularJs application

I am currently trying to implement the tinyColorPicker plugin from here in my Angular app, but I am facing difficulties with it. An error message keeps appearing: TypeError: element.colorPicker is not a function In my index.html file, I have included th ...

Continuously iterate through a PHP page until the session variable reaches a specific value

I am in the process of creating a web-based exam. All questions and answers are stored in a MySQL database. I began by retrieving and displaying one question along with multiple-choice answers. I used a session variable $_SESSION['questionno'] = ...

Innovative sound system powered by React

I am working on implementing a music player feature on a website where users can select a song and have it play automatically. The challenge I am facing is with the play/pause button functionality. I have created all the necessary components, but there see ...

Guide to encoding an object containing multiple arrays of objects into JSON using PHP

My goal is to generate a JSON structure as shown below: { "success":[{ "success": "1" }], "friends": [ { "name": "ali", "phone": "934453" }, { "name": "reza", "pho ...

Adding a promise to an array using Javascript

I am facing an issue while attempting to create an array of promises and then calling them using Promise.all. The problem lies in correctly pushing the functions into the array. It seems like they are getting executed instead of being inserted and waiting ...

How to fetch images from a database in CodeIgniter by utilizing JSON and AJAX functions?

Trying to retrieve an image using ajax/json format for the first time, all variables are displaying except the image. The name of the image is visible when inspecting the element in the browser and it is saving correctly into the image folder. I need help ...

What's the best way to trigger useEffect whenever a useRef variable gets updated?

I am facing an issue where the variable changes are lost on every re-render when trying to have useEffect run every time it modifies a variable. Using useRef to store the variable between renders leads to useEffect not detecting changes to a ref. I came a ...

Incorporating JavaScript Variables into MySQL Database with AJAX: A Step-By-Step

Looking to implement the solution provided in this query about adding records into a database using php/ajax/mysql: Best way to add records into DB using php/ajax/mysql? This is my current code: JavaScript function FromFlash(m1, m2, m3, m4){ var po ...

The Bootstrap modal form fails to properly handle the POST method when sending data to the server

I am encountering an issue with a button that triggers a modal form <a href="#" class="btn btn-primary" data-toggle="modal" data-target="#agregarProducto">Add Material</a> The modal appears as shown below: https://i.stack.imgur.com/J39x9.pn ...

How can I trigger an audio element to play using onKeyPress and onClick in ReactJS?

While attempting to construct the Drum Machine project for freeCodeCamp, I encountered a perplexing issue involving the audio element. Despite my code being error-free, the audio fails to play when I click on the div with the class "drum-pad." Even though ...

Showing JSON data in a popup and returning an ActionResult

I have created a custom controller in MVC with the following ActionResult: [HttpPost] public ActionResult Details([DataSourceRequest]DataSourceRequest command, int id) { //var userDetail = _CustomerDetail.GetAllCustomers(); var g ...

I'm having trouble figuring out why req.param('length') keeps returning 0

In my current project using Sails.js (built on top of Express), I encountered an issue with sending a form input named length with a value of '1000'. Here is how it was implemented: <select name="length"> <option value="1000">100 ...

Determine the Number of NaN Values in Each Column of an ndarray

Issue: I am working with a numpy ndarray of size (2000,7) and I need to count the number of NaN values in each column and store them in a new ndarray Attempted: number_nan_in_arr = [np.count_nonzero(np.isnan(arr[:,i])) for i in range(arr.shape[1])] Howev ...