In the absence of an Avatar, verify the gender from the JSON data and insert it into the image

I have created my very first app where users can upload their avatars and set their gender. If a user (in this case, a girl) does not upload an avatar, I want to check the JSON data for their gender information. Based on this gender information, I want to display the URL to the default female avatar.

Take a look at my Controller Code:

$scope.getUserImage = function (user) {
    if (user.avatar) {
        return user.avatar;
    } else {
        return '/images/icons/user-' + user.gender + '.svg';
    }
};

$scope.schueler = [
    {
        firstName: 'Sven',
        lastName: 'Wauback',
        gender: 'male',
        avatar: 'url to avatar',
        notice: 'Almost done with theory'
    },
    {
        firstName: 'Sonja',
        lastName: 'Flockenbarsch',
        gender: 'female',
        avatar: 'url to avatar',
        notice: 'Needs extra practice hours'
    },
    {
        firstName: 'Lisa',
        lastName: 'Weber',
        gender: 'female',
        avatar: '',
        notice: 'Not interested in photo submission'
    },
    {
        firstName: 'Manuel',
        lastName: 'Bürstenkrieger',
        gender: 'male',
        avatar: 'url to avatar',
        notice: 'Thinks he's the coolest'
    }
];

Now let's take a look at the View File:

<md-list-item ng-repeat="item in schueler" class="md-2-line" ng-click="null">
    <img ng-src="getUserImage(user)" class="md-avatar" alt="{{item.firstName}} {{item.lastName}}"/>
    <div class="md-list-item-text" layout="column">
        <h3>{{item.firstName}} {{item.lastName}}</h3>
        <p>{{item.notice}}</p>
    </div>
    <md-divider></md-divider>
</md-list-item>

I attempted to create a function above, but it doesn't seem to be working. As I am not familiar with JavaScript, I believe my approach is incorrect. Can someone guide me on the correct way to achieve this?

Answer №1

It appears that the incorrect value is being passed for the image source. Isn't it supposed to be getUserImage(item) instead of getUserImage(user)?

Edit: To retrieve the value, use ng-src="{{getUserImage(item)}}"

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 could be causing my TypeScript code to not be recognized as CommonJS?

I rely on a dependency that is transpiled to ES6. My goal is to leverage ES2019 features in my own code. Ultimately, I aim to output ES6. This is how I set up my tsconfig { "compilerOptions": { "module": "CommonJS" ...

Differences between JSX and creating instances of component classes

Could someone please clarify the distinction between the following two statements? let instance1 = new CustomComponent(); and let instance2 = <CustomComponent /> When checking in the Chrome debugger, I see the following: for instance1 CustomComp ...

Troubleshooting Cross-Origin Read Blocking with the Google Maps Elevation API using Axios in a Vue.js Application

I'm currently working on integrating the Google Maps API into a Vue.js project. I've encountered an issue with two Google Maps services: - The Time Zone API is functioning properly. - However, the Elevation API is giving me a Cross-Origin Read Bl ...

Extracting multiple values from a JSON object with an array as the root in Swift 5

As a newcomer in the world of app development, I am embarking on my first iOS project which involves creating a currency table/converter for Ukrainian Hryvna. To populate a TableView with data, I plan to retrieve information from a JSON file accessed throu ...

The art of creating an asynchronous function: A comprehensive guide

My goal is to download files from a Firebase bucket and then store them in a database. I need the download process to be asynchronous, ensuring that each file is fully downloaded and added to an array before moving on to the next one. However, my current ...

Having trouble with Electron nodeIntegration functionality and experiencing some odd behavior in general with Electron

Having just started working with Electron, I find myself encountering some puzzling behavior that has me stumped. Here's a quick summary of the issue: I am unable to establish communication between Electron and the HTML "Uncaught ReferenceError ...

utilization of dynamic templates within directives in the AngularJS framework

When it comes to deciding on a template based on the date, I came across an interesting example. However, in that specific example, the templates were so simple that strings could have been used. In my case, I prefer using PHP to generate the templates, so ...

Attempt to import Recharts into React was unsuccessful

I am currently exploring the use of recharts in a React project, but I am facing difficulties when trying to import components from its library. I have added it to my package.json file as follows: "recharts": "^2.0.9", However, I am ...

HTML - Retain placeholder text while user inputs

My input is structured like this: <input value="My text" placeholder="Placeholder"> Typing in the input causes the placeholder text to disappear, which is expected. However, I am looking to keep the placeholder text visible as a background behind ...

Using the .ajax() function with jQuery to retrieve data and then using .find() on that data

Currently I am facing an issue while trying to extract the body tag from the result of the .ajax() call. Instead of getting the desired result, I only see undefined logged into the console... This is the section of code causing the problem: $(document).r ...

Issues with importing Three.js as a module - encountering an Uncaught SyntaxError:

I am currently delving into the world of three.js and working on my first project. I am following the example code provided on the three.js website. Everything runs smoothly when I have three.js stored in a folder like: js/ directory However, I am enco ...

Is it possible to redefine a function that is attached to $ctrl outside of the AngularJS framework?

Within my DOM, there exists an element containing ng-click="$ctrl.goToHome(), which is connected to the logo on my site. This particular element is generated by a third-party AngularJS application. The complete element can be seen below: <img ng-if=":: ...

Is there a way to determine the position of the highlighted text within a textarea?

Is there a simple way to calculate the position of the selected text within a textarea using vanilla JavaScript or Angular (possibly with a directive)? This is important in order to display a div with a popup above the selected text. Coordinates are need ...

data.map` does not work as a function

I've encountered a persistent error that has me stumped. Here's the code snippet: JSON {"inventory": [ { "item_id" : "123", "item_data" : { "image_id" : "1234", "description" : "foo", "lin ...

Attempting to use Vue.js for playing MP3 files

Motive: My objective is to incorporate a background sound into my project using a local file that can be played and paused. While loading an external URL file works fine and allows for play/pause functionality, I am encountering issues with the local fil ...

Arranging nested arrays

There is a nested list provided with the following markup (which cannot be altered currently). My goal is to sort this list and all nested lists by the title of the 'a' tag. The initial div (not nested in 'li') should be used for sortin ...

Unable to display returned data from an AJAX request made with jQuery autocomplete

After reviewing the debug developer tool, I noticed that the ajax request returned data but for some reason, the data is not being displayed in the text box. The data contains special characters which are visible in the provided image. I am trying to iden ...

What is the process for uploading an image with express-fileupload?

Looking to upload an image to Cloudinary via Postman using the express-fileupload library for handling multipart forms. Here is a snippet from my index.ts file: import fileUpload from "express-fileupload"; app.use(fileUpload()); In my controller ...

How can multiple Angular service calls be queued to wait for completion simultaneously?

Two controllers in my application require the same data at approximately the same time, but I want to avoid binding the data. To handle this scenario, I have implemented a service that retrieves user data using the following method: var getUserData: func ...

Guide for integrating images in React Native

I am struggling to display images in a carousel properly. I attempted to require them using JSON like this {"Image": require("$PATH")} or in a js file within the ParallaxImage tag, but nothing seems to work... Item Creator _renderItem ...