Tips on how to hide items from printing if they are not true in Vanilla JavaScript/TypeScript and Angular 9

How do I prevent interactions from being printed if the value is a number?

Answer №1

Go through the selected items one by one. This means avoiding the use of book.links in ngFor and instead using a custom array that excludes any empty items.

For example:

<button *ngFor="let singleBook of validLinks" type="button">  
  ...
</button>

Also utilize a getter in the component like this

get validLinks () {
  return this.book.links.filter(b => !!b.trim())
}

Answer №2

How to clean up values

sanitize(value){
  return value.split('/').pop().replace(/-/g, " "); 
}

get cleanedBookLinks() {
 return this.book.links
 .map(item=> ({...item,  link:this.sanitize(item.href)}))
 .filter(item=> !this.isNumeric(item.href));
}

Displaying in the HTML template

<button type="button" 
      *ngFor="let singleBook of cleanedBookLinks">  
       <span (click)="displayInfo(book, $event, i)" >
          {{ singleBook.link }} 
       </span>
   </button>

Answer №3

To avoid using multiple structural/conditional directives on the same element (ngif + ngfor), a solution is to wrap them in an ng-container.

<ng-container *ngFor="let singleBook of book.links">
  <button type="button" *ngIf="selectedAction(singleBook.href)">
   <span (click)="showI(book, $event, i)">
      {{ selectedAction(singleBook.href) }} 
   </span>
  </button>
</ng-container>

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 it possible to retrieve local variable JSON arrays using ajax/getJson()?

When starting a project without a database or data source, I often create json arrays in a *.js file to populate screens until the data modeling or database creation is complete. I am trying to figure out how to write an ajax/getJson() function to access ...

The Angular single-page application fails to refresh when being executed via Visual Studio 2017 Community

I have encountered a problem with my angular 6 app not refreshing when running through Visual Studio 2017. The project consists of multiple projects, including a .NET Core 2 WebAPI and the angular app in question. Even after setting the startup to only be ...

Experiencing a failure in defining the factory

I'm currently working on passing data between controllers in Ionic with Angular. I know that using a factory is the best approach for this, but I keep encountering an error: ReferenceError: setData is not defined This is my code: app.factory("Pla ...

I'm having trouble getting my application to output to the console. What could be the issue?

In my cr-route.js file, I have a function that ensures the user is authenticated before displaying their name. module.exports = function (app, passport) { // ===================================== // HOME PAGE (with login links) ======== // == ...

Using leaflet-markercluster in Angular 2 with Leaflet

Having some trouble trying to configure leaflet with the leaflet-markercluster plugin using angular 2. Although I've managed to make it work with angularJS in the past, I'm facing issues in angular and could use some guidance on what might be cau ...

Exploring Facebook Graph Data with Meteor 1.3

During my time using Meteor 1.2, I utilized the following code on my server to access user data and friends data: function Facebook(accessToken) { this.fb = Meteor.npmRequire('fbgraph'); this.accessToken = accessToken; this.fb.setAcc ...

Tips for creating a div element that closes on the second click:

On the PC version, I have three blocks that open and close perfectly when clicked. However, on the mobile version, when I click on one block, it opens but does not close unless I click on another block. Additionally, if I click again on the same block th ...

Unable to choose anything within a draggable modal window

Can someone please assist me? I am struggling to figure this out. I can't select text inside the modal or click on the input to type text into it. I suspect it may be due to z-index issues, but I'm having trouble locating them. var currentZ = n ...

Creating a type-safe method wrapper in TypeScript based on function names

Many Q&As discuss creating a function wrapper in TypeScript, but my question is how to do the same with named methods. I am interested in writing something similar to this JavaScript code: function wrap(API, fnName, fn) { const origFn = API.p ...

Having trouble with Angular2 testing? It seems like you may be missing the 'angularCli.config' entry in your Karma configuration

When I attempt to run 'npm test' in my Angular2 project, I encounter the following error: "Missing 'angularCli.config' entry in Karma config" Does anyone have any insights on how to resolve this? ...

Refreshing a Thymeleaf table dynamically without having to reload the entire page

I am currently using the Thymeleaf attribute to render data, but I am now looking to add a "Search" button without reloading the page. Within the attribute departments, I am rendering a List<Department> from the database. While I understand how to a ...

Utilizing Async and await for transferring data between components

I currently have 2 components and 1 service file. The **Component** is where I need the response to be displayed. My goal is to call a function from the Master component in Component 1 and receive the response back in the Master component. My concern lies ...

The function jquery__WEBPACK_IMPORTED_MODULE_4__.hubConnection is not recognized

I am currently trying to integrate singlar (not ASP.Core) with angular 8. If I directly include signalr and jQuery in index.html as follows: <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWS ...

What's the best way to create an onclick event for a li element that includes a pseudo-element ::before

I have successfully created a Timeline using HTML and CSS elements. The visual result is as follows: My goal is to enable users to click on the second circle, triggering a color change in the line that connects the first and second circles. This color tra ...

Create a generic function that retrieves a specific property from an array of objects using the select method

Currently, I have implemented four functions that select entries from an array based on different properties. if ($scope.filters.filter1) $scope.filteredEntries = $scope.filteredEntries.filter(function (o) { return o.field1 === $scope.filt ...

"Utilizing JQuery's addClass, removeClass, and appendTo functions to

Having a bit of an issue trying to implement a functionality where options inside a div can be clicked and moved to another div, and if they are clicked again in the other div, they will return back. Here is the code snippet: $(".selectable").bind(&ap ...

What distinguishes v-text from v-load in Vue.js when concealing {{ Mustache }}?

When experiencing the "flash of uncompiled content" in Vue, the brief moment when the page is loading and you see the {{ Mustache }} syntax, developers often use either v-text or v-cloak. According to the documentation, v-text: Updates the element’s t ...

Tips for passing a variable containing an image source from Node.js to a Jade file

Below is the code snippet from my index.js file, where I'm using express for routing: var express = require('express'); var router = express.Router(); /* GET home page. */ router.get('/', function(req, res){ var db = req.db; ...

Angular 2 can efficiently generate multiple HTTP requests simultaneously from a single HTTP request

Backend API's: url: [ { "name": "ABC", "occupation": "Student", "address_url": "http://www.sample.com/address/person=hgjgjgyyfhg" }, { "name": "ABC1", "occupation": "Carpenter", "address ...

How to Dynamically Load SVGs in Angular 10 Without Using IMG or OBJECT Elements

I have been exploring a more streamlined method for loading SVG files without relying on IMG or OBJECT tags, as it limits my ability to control fill colors through external CSS. While using inline SVG seems like the best option, managing numerous component ...