Steps for including a URL in an ng-repeat when it is not included in the JSON object

After making a http call, I am getting a JSON object. One of the properties within this object is LinkUrl. Sometimes, LinkUrl will contain a predetermined URL from the returned object, while other times it will only have a file name. In the latter case, I will need to add a base URL to the beginning of the file name.

I'm unsure whether there is a more angular-specific method to handle this scenario or if I should use a regex if statement.

Below, I have included the LinkUrl that only contains the file name. If there is no URL provided, I will need to prepend a URL specifying where the file will be hosted. This will result in the output being displayed in my view.

Any assistance on this matter will be highly appreciated.

 0: Object
$$hashKey: "00B"
GroupId: "1"
GroupName: "Store # 11 - OKC"
LanguageName: "English"
LinkTypeAbbr: "Store_Docs"
LinkTypeDescr: "Store Documents"
LinkTypeId: "1"
LinkUrl: "store11_OKC_Docs.pdf"

Answer №1

If you want to achieve this, you could try

<a href="{{obj.LinkUrl.startsWith('http://') ? obj.LinkUrl : baseUrl + obj.LinkUrl}}" >{{obj.GroupName}}</a>

Answer №2

One way to validate if a field is a URL is by using regular expressions. You can refer to this helpful answer for more information: (shared code provided below)

function CheckURL(input) {
  var pattern = new RegExp('^(https?:\/\/)?'+ // protocol
    '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
    '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
    '(\\:\\d+)?(\/[-a-z\\d%_.~+]*)*'+ // port and path
    '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
    '(\\#[-a-z\\d_]*)?$','i'); // fragment locater
  if(!pattern.test(input)) {
    alert("The provided URL is not valid.");
    return false;
  } else {
    return true;
  }
}

if (!CheckURL(yourData.UrlField)) {
  yourData.UrlField = defaultAddress + yourData.UrlField
}

Answer №3

<a href="{{checkLink(obj.Url)}}">file</a>

$scope.checkLink = function(url){
     /*This function checks if the URL contains HTTP.
      If it does not contain HTTP, a final URL is generated and returned.
     */
return finalURL;
}

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

The implementation of async await within a switch case statement is failing to function properly

Is it possible to wait for the resolution of a promise within a switch case statement by using the keyword await? I am facing an issue with my Angular component where the following code is causing crashes in my application. switch (this.status) { ...

Axios cancel token preemptively cancelling request before it is initiated

Currently, I am working on implementing cancellation of axios calls in my project. After reviewing the axios documentation, it appears to be quite straightforward, which can be found here. To begin, I have defined some variables at the top of my Vue compo ...

Could someone provide some clarification on this callback related to node.js?

With the abundance of node.js tutorials available showing how to create a server, it can be overwhelming as they are all coded in different ways. The question then arises - when should you write it one way versus another? Unfortunately, none of the tutoria ...

How to send a JavaScript variable to Flask and trigger an AJAX reload action

Introduction: I have explored similar inquiries and attempted to apply relevant code/concepts but without success. -https://stackoverflow.com/questions/43645790/passing-javascript-variable-to-python-flask -https://stackoverflow.com/questions/10313001/is- ...

Imitate a worldwide entity with Jest and Typescript

I have a React component that utilizes the global object Routes provided by the Rails gem js-routes. My component is being tested with Jest's snapshot testing feature. The issue arises when trying to mock the Routes global object in the test to return ...

Unable to invoke parent method from child component in Vue causing issue

I am facing an issue where I am trying to trigger a method in the parent component from the child component using $emit, but for some reason, it is not working as expected. Can someone please help me troubleshoot this problem? Parent Component <templat ...

Tips for preserving scroll position within a division following the redisplay of a division in Vue.js

Within my Laravel and Vue project, I have set up a feature to display posts in a scrollable area. This area is only visible when the 'showFeed' variable is true. <div v-show="showFeed" class="scroll-container" v-scroll=&quo ...

What is the best way to animate various sprites using distinct buttons in HTML and CSS?

I've been experimenting with animating a sprite by using different onClick button triggers. I encountered an issue where only one of the buttons works correctly in the fiddle. However, in my local file version, the other buttons work but only once and ...

Guide to connecting two separate components from distinct pages in React/MUI

My setup involves two pages: Appbar.js, where I have a top appbar and a Navigation Menu Icon Button that triggers the display of my Drawer (Material UI) in TempDrawer.js. Initially, everything worked fine when all the code was placed in the Appbar.js file ...

Instructions on creating a non-editable text area where only copying and pasting is allowed

I am looking to simply copy and paste content into a textarea without needing to make any edits within the field. <form action="save.php" method="post"> <h3>Text:</h3> <textarea name="text" rows="4" cols="50"></textarea ...

Immersive Visual Symphony through Dynamic Multi-Layered

My goal is to create captivating animations for my multiple background images. Here's the CSS code I've come up with: .header { background-image: url('/img/cloud2.png'), url('/img/cloud3.png'), url('/img/cloud1.png&apos ...

Tips for refining SQL query output by matching specific strings

I am working with a database table that includes a column storing string values resembling PHP dictionaries. Table: College Id name requirement 1 x {"users": ["A", "B"], "name": "*", "pool": "CSE", "place": "Bangalore"} 2 y {"users": ["A", "C ...

Is there a way to modify the output Json during serialization on a broader scale, even if the class definitions are not modifiable?

Suppose the following: You have a collection of Java objects that need to be passed to an API You are unable or unwilling to modify the declaration of these objects Unfortunately, the API requires additional information not present in the objects For in ...

Firefox and Internet Explorer do not support the functionality of HTML5 <audio> tags

My radio stream player code works perfectly on Chrome and iOS Safari, but I'm facing compatibility issues with Firefox and Internet Explorer 11. Here's a snippet from my index.php file: <script type="text/javascript" src="http://code.jquery. ...

Rearrange the items in an array that contain different data types

After spending some time searching on SO, I did find some helpful information but I am still struggling to achieve the desired solution. My current issue involves a keypad with numbers 1 through 5. When a number key is selected, it gets added to an array n ...

Is there a way to remove specific mesh elements from a scene in Unity?

When I create multiple mesh objects with the same name, I encounter difficulties in selecting and removing them all from the scene. Despite attempting to traverse the function, I have not been successful in addressing the issue. event.preventDefault(); ...

Encountering Issues When Asserting Text with Selenium

Hello, I am attempting to verify text from a website and have included the image below. The text contains a BR tag as you can see. Despite the fact that both texts are identical, I encounter an error when trying to assert the content. Here is my code snipp ...

The data retrieval process with angularjs is not functioning properly

While working on my web application project, I encountered an issue with integrating angularjs. I have a table named city which was created using entityframework and database first. To address this, I included the angularjs nuget package in my project. Ad ...

In ReactJS, ensure only a single div is active at any given moment

I'm working on a layout with three divs in each row, and there are multiple rows. Only one div can be selected at a time within a row, and selecting a new div will automatically unselect the previously selected one. Here is a simplified version of my ...

ReactJS presents an issue where buttons on the navigation bar are not properly aligned

I am currently utilizing the navbar component from Bootstrap 5, and I've encountered a UI problem. What is my current setup? This is the existing structure of my navbar: https://i.sstatic.net/eETzd.png What is my desired outcome? I aim to have the n ...