Unable to display image source in viewport

Currently, I am working on developing a basic ionic app that interacts with an API that I have created. I am encountering an issue where all data is being displayed correctly in the view except for the src attribute of an image. When I use console.log to check the data in my controller, the src is present but it appears as a 0 in the view, like this:

<img ng-src="0" src="0">

In my controller, I am passing the data like this:

.controller('VenuesController', ['$state','$scope','$http','Venue', function($state,$scope,$http,Venue){
    $scope.venues = Venue.query();
    $scope.showVenue = function(id){
        $state.go('venues/:id',{id:id});
    };
}])

And in the view template itself:

<ion-view view-title="Venues">
    <div class="list">
        <a ng-repeat="venue in venues" ng-click="showVenue({{venue.id}})" class="item item-thumbnail-left">
          <img ng-src="{{ venue.image-small }}">
          <h2>{{ venue.name }}</h2>
          <p>{{ venue.description }}</p>
        </a>
    </div>
</ion-view>

The image path is a full external link such as

http://lorempixel.com/100/100/?51467
, and I'm unsure if I've overlooked something here?

Answer №1

venu.image-small variable has a character that is not allowed in variable names, such as a hyphen (-). Variable names should not contain special characters like hyphens or start with numbers.

To access the property image-small which contains a hyphen (-), you should use array notation like this: venue['image-small']

Markup

<img ng-src="{{ venue['image-small']}}">

Update

If your image source URL is from a different domain than your current one, you need to trust the external URL by using the $sce service's trustAsResourceUrl function.

Markup

<img ng-src="{{ trustSrc(venue['image-small'])}}">

Code

$scope.trustSrc = function(src) {
   return $sce.trustAsResourceUrl(src);
}

Additionally, avoid using {{}} interpolation directive inside ng-click

ng-click="showVenue(venue.id)"

Instead, update your function implementation like this:

$scope.showVenue = function(id){
    // Replace `venues/:id` with the appropriate stateName
    $state.go('stateName',{id:id}); 
};

Improvement can be made by using ui-sref directive for redirection like:

Final Markup

<a ng-repeat="venue in venues" ui-sref="stateName({id:id})" class="item item-thumbnail-left">
    <img ng-src="{{ trustSrc(venue['image-small'])}}">
    <h2>{{ venue.name }}</h2>
    <p>{{ venue.description }}</p>
</a>

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

Is there a way to trigger a custom event from a Web Component and then intercept it within a React Functional Component for further processing?

I'm facing an issue with dispatching a custom event called "select-date" from a custom web component date picker to a React functional component. Despite testing, the event doesn't seem to be reaching the intended component as expected. Below is ...

AngularJS checkbox validation requires a minimum of two checkboxes to be selected

When using AngularJS, I am looking to create a validation rule where a minimum of 2 checkboxes must be checked for the input to be considered valid. Here is what I have attempted: <div ng-repeat="item in items"> <label><input type="chec ...

Deleting an added element upon closing the dialog box

Utilizing JQuery and Ajax, I successfully update my database. Following each update, a png icon is displayed briefly for 1 second. Specifically, the update form is contained within a JQuery dialog box. However, upon closing the dialog box after an update, ...

What is the best way to consistently and frequently invoke a REST API in Angular 8 using RxJS?

I have developed a REST API that retrieves a list of values. My goal is to immediately invoke this API to fetch values and store them in a component's member variable. Subsequently, I plan to refresh the data every five minutes. Upon conducting some ...

The response from $http.get is not defined

Currently, I am working on the front end of a project using HTML, while utilizing Python for the back end. To handle communication between the two, I have integrated AngularJS. The challenge I am currently encountering pertains to retrieving responses fro ...

When executing a function, the previous React state continues to linger

Why is the updateUser() function only updating the last user instead of all users despite using useCallback and including users as a dependency? The expected output after clicking the update button should be: {"id":1,"name":"John& ...

Problems encountered when transferring information from jQuery to PHP through .ajax request

Hey there! I am currently working with Yii and facing an issue while trying to pass some data to a controller method called events. This is how my jQuery ajax call looks like: var objectToSend = { "categories" : [selectedOption],"datefrom" : month + "" + ...

Ensure that the <TabPanel> content occupies the entire width and height of its parent container

Currently, I am working with React and material-ui. Specifically, I am utilizing an appbar with tabs and my goal is to have the content of each tab expand to full width and height when selected. If you'd like to see an example, check out this sandbox ...

Utilizing d3.js to implement a scatterplot with zoom functionality that focuses solely on zooming the axis without affecting

Having trouble creating a scatterplot with zoom functionality where only the axis is getting zoomed, not the data itself. Can anyone provide some assistance or insight on what might be wrong? If you're interested in checking out the project, here&apo ...

What is the method to retrieve the string value from a JavaScript String object?

Is there a way to extend a method to the String prototype and manipulate the string value? I'm facing some difficulty in accessing the actual string value, as this, the current object context, seems to refer to the string object instead. String.pro ...

Utilizing precise data types for return values in React hooks with Typescript based on argument types

I developed a react hook that resembles the following structure: export const useForm = <T>(values: T) => { const [formData, setFormData] = useState<FormFieldData<T>>({}); useEffect(() => { const fields = {}; for (const ...

Setting the initial value of the array to display as a dropdown menu option

I am trying to set the default value of a json array object in a drop down list <select ng-model="selectedItem" ng-options="item as item.taskName for item in abc.taskList" ng-init="selectedItem = selectedItem || abc.taskList[0].taskName"> <pr ...

Having trouble sending a JavaScript variable to PHP via AJAX

I am attempting to pass a JavaScript variable (a value obtained when a user chooses a random option from a select dropdown menu) into a PHP variable in order to check the attributes of the selected option in my database. The option corresponds to the name ...

"Troubleshooting issue with AngularJS ng-repeat order by functionality based on

I am currently trying to sort my ng-repeat by descending date order, with the newest items appearing first. Despite my efforts, I have been unable to achieve this. I have carefully checked for any errors in quoting, but still no luck. I've attempted ...

Guide on sending a personalized email to multiple recipients using office.js

Is it possible to send individual emails when using office.js in Outlook for mail delivery? For instance, if there are 5 recipients, can the email be sent so that each recipient only sees their own email and not others'? This is unrelated to BCC fun ...

Transferring information within AngularJS modules and controllers

I'm facing an issue where I have two modules, each with its own controller, and I need to pass an object between them. I've tried using a service, but I keep running into an "$injector" error in the second module/controller. Can someone please he ...

A guide on utilizing webpack devServer proxy within a create react app

Currently, I am in the process of developing a new application with create-react-app and I am looking to incorporate some proxies into my code. In the past, I utilized webpack's devServer for this purpose. module.exports = { ... devServer: { ...

What is the best method for extracting attribute values from multiple child elements using puppeteer?

When using this code, I can retrieve the attribute value of the first element selected. By adding /html/body/section/div[3]/img<2> or img<3> in the xpath, I am able to retrieve data for subsequent img elements. However, the parent element on t ...

altering the color of various spans consecutively

I am looking to create a text effect where each alphabet changes color in a wave-like pattern, starting from the left. I have assigned each alphabet a span with classes like span0, span1, and so on. To change the color, I used the following code: for (var ...

Managing promises with mongoose - Best practices

I am new to using mongoose and I am trying to figure out how to save and handle promises in Node.js using a mongoose schema. In the example below, I am attempting to save data to a collection and handle any errors that may occur. model.js var mongoose = ...