Attempting to assign a default background image with the help of Angular.js

Recently delving into Angular.js, I've encountered an issue. Utilizing ng-repeat for iterating through a list of news items, each with Title and Body properties, some may also contain an optional picture URL to be used as the background.

I have managed to display news item elements with picture URLs appropriately when there is a valid URL available. Sample code snippet provided below:

<li class="news-item col-md-6" ng-repeat="announcement in news.announcements | limitTo:8" ng-style="{'background-image': 'url({{announcement.Url}})'}">

The challenge lies in setting announcement.Url value to a default background picture URL when it is NULL or Undefined. Seeking guidance on how to approach this scenario.

Your assistance would be greatly valued.

Many thanks!

Answer №1

<li class="news-item col-md-6" ng-repeat="announcement in news.announcements | limitTo:8" ng-style="{'background-image': 'url({{announcement.Url || \'path to default image\'}})'}">

When working with angular expressions, anything between {{ and }} should be considered. This is similar to javascript for beginners. By using the || operator (logical OR), you can define a default path for the image in case announcement.Url is null or undefined.

Edit: It's important to escape string delimiters (\') when they are nested inside a string that already uses those delimiters.

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

Verifying X-editable input in Laravel (Server-side)

I've successfully integrated x-editable into my webpage (). When I click on a field, it becomes editable inline, allowing me to make changes and submit them to a POST URI. Currently, I am sending three key-value pairs: array:3 [▼ "name" => "n ...

Trapping an anchor tag event in asp.net

I am currently working on a menu bar using HTML code (I am unable to use asp link buttons). <ul> <li><a href="#"><span>Reconciliation</span></a> <ul> ...

Converting a jQuery function into AngularJS: A step-by-step guide

I'm completely new to AngularJs and I recently created a slider function using jQuery. Now, my goal is to convert this function into Angular. Below is the code snippet that I have: <div class="slide-container"> <div class="slide-s ...

Pattern matching to extract multiple numbers within a string

I need help extracting numbers from a string like: There are 1,000 people in those 3 towns. I want to create an array like ["1,000", "3"]. I found a number matching Regex from Justin in this question ^[+-]?(\d*|\d{1,3}(,&b ...

Keep the sub menu open by clicking within the submenu

Is there a way to prevent the menu from moving up when I click inside the submenu? (The submenu will slide up when you click on the main link or outside of it.) Is it possible to achieve this without using javascript? And then have the menu overlap and fun ...

Is it possible to implement a while loop in React?

Issue: I am facing a challenge while attempting to generate an array of 4 elements from a list using a while loop, but it seems to result in an infinite loop. const [options, setOptions] = useState([]); const fetchElements = () => { while(options. ...

Guide to making a Java Servlet for a specific jQuery.ajax () request?

In one of my files named wfd.proxy.js, I have the following code snippet: if (!WFD) { var WFD = {}; }; if (!WFD.Proxy) { WFD.Proxy = {}; }; WFD.Proxy = { SERVICE_URL : "/delegate/WFD/WFService?", PDF_SERVICE_URL : "/delegate/pdf-exporter?", ...

Using Single Quotes as Parameters in JavaScript

I'm currently facing an issue with a function that is designed to populate a field in the parent window when clicked. Specifically, it is meant to fill in a text field with a name. The challenge I am encountering arises when the field contains a sing ...

Issue with updating AngularJS model within nested ng-repeat block

My issue involves using nested ng-repeat to iterate through properties of an array of objects and connecting the model of the objects' values to inputs. The problem arises when the values inside the inputs are changed, the model fails to update. va ...

Incorporate an HTML code string into an Angular 2 template

I am working with HTML code stored in a Component variable, shown below: import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: `Hi <div [innerHTML]="name"></div>`, styleUrls: [' ...

"Learn how to dynamically bind static data from an AJAX call using jQuery in a seamless

Hey there! I'm fairly new to jquery and I have a graph that currently receives data statically. However, I'd like to make it dynamic instead. Here's the current static data: var data = [ { label: "New Request", data: 1.7, color: "# ...

Angular js encountered an unexpected error: [$injector:modulerr] ngRoute

https://i.sstatic.net/PATMA.png In my Angular code, I encountered the error "angular is not defined". This code was written to test the routing concept in AngularJS. var app=angular.module('myApp',['ngRoute']) .config(function ($routeP ...

Error thrown: Openerp 7.0 fails to read the prototype property of null due to a TypeError

Using Openerp (Version 7.0-20140429-231256) has been smooth sailing until I encountered a sudden error one day that left me stumped. Despite my efforts, I couldn't find any solutions to resolve this exception. OpenERP Client Error Uncaught TypeError: ...

React component triggering dynamic redirection based on URL query changes

I've been struggling to implement a redirect with a query from a form in React. Despite trying different methods, I haven't been able to make it work. As a newbie in front-end development, what might seem simple to others is actually causing me s ...

Load HTML content dynamically based on the output of a PHP script

I am attempting to display the output of a PHP file in an HTML div. I have used jQuery's ($('div').load(url_here)) method and it is functioning correctly. However, the code of my PHP file is as follows: <?php for( $i = 0 ; $i < 10 ; $ ...

Strapi and Next.js website experiencing issues with updating blog content

I currently have a Next.js application running in production that is powered by Strapi for content management. The content is fetched using the getStaticProps function on each of the two pages in my small blog. However, I've noticed that when new con ...

What is preventing images from displaying in my slider within baguetteBox?

Currently, I am in the process of incorporating a slider into my website using the baguetteBox library. After consulting the documentation, I successfully implemented an example that is functioning smoothly without any issues. In order to display a series ...

Tips for creating a smooth transition effect using CSS/JavaScript pop-ups?

Looking for some assistance in creating a CSS pop-up with a touch of JavaScript magic. I've managed to trigger the pop-up box by clicking a link, and while it's visible, the background fades to grey. But I'm struggling to make the pop-up fad ...

When material skinning is enabled, the skinned animation mesh in Three.js mysteriously vanishes

After exporting an animated model from Blender, I encountered an issue with instantiating it in my project. Although I was able to create the THREE.Animation and model, I found that the animation was not working. I soon discovered that I needed to set skin ...

Using jQuery to display a div after a 2-second delay on my website, ensuring it only appears once and does not reappear when the page is refreshed or when navigating to a

I manage a website that includes a blog section. Every time someone visits the site, I want a popup window to appear. (To achieve this, follow these steps - Utilize jQuery for showing a div in 5 seconds) I would like this popup to only be displayed once ...