"Omitting the parameter value when passing it to a directive in Angular

I recently developed a Directive in Angular7, but I encountered an issue when trying to pass a string value from the HTML to the directive.

When using the following code snippet in my HTML:

<ng-template [appValidatePermission]='CreateRole'>
   <router-outlet></router-outlet>
</ng-template>

This is the code for my Directive:

    @Directive({
  selector: '[appValidatePermission]'
})
export class ValidatePermissionDirective implements OnInit {

  show: boolean;
  constructor(private templateRef: TemplateRef<any>,
              private viewContainerRef: ViewContainerRef
    ,         private dynamic: DynamicPermissionService) { }

  // tslint:disable-next-line:no-input-rename
  @Input() AccessName: string;

  ngOnInit() {
    this.ValidatePemission();
    if (this.show) {
      this.viewContainerRef.createEmbeddedView(this.templateRef);
    } else {
      this.viewContainerRef.clear();
    }
  }
  ValidatePemission() {
    console.log('AccessName : ', this.AccessName);
    const find = this.dynamic.dynamicModel.find(x =>
      !! x.actionsVM.find(z => z.actionEnglishName === this.AccessName));
    if (find) {
        this.show = true;
      } else {
         this.show = false;
      }
  }
}

However, upon testing, it displayed AccessName: Undefined.

What could be causing this problem and how can I resolve it?

Answer №1

The variable is not being passed correctly. In order to use a directive in your HTML, follow this format:

  <ng-template appValidatePermission AccessName="CreateRole">
   <router-outlet></router-outlet>
   </ng-template>

Keep in mind that if you want to pass a string using [], you should do it like this:

  <ng-template appValidatePermission [AccessName]="'CreateRole'">
   <router-outlet></router-outlet>
   </ng-template>

Answer №2

Give this a shot

<ng-template [Data]="<value you wish to transmit>" appValidatePermission >
   <router-outlet></router-outlet>
</ng-template>

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 update the value of an <input> element dynamically?

There is an input in a datalist that I am working with. <input type="text" value="1" id="txtcount"> I am trying to obtain the new value of the input when the text changes. I attempted to use the following code, but it did not work as expected. &l ...

What are the best scenarios for utilizing (a)synchronous calls?

Currently, I am working on a project for my office as a learning exercise. We have a tradition of bringing food every Monday, so I decided to create a program that displays each person's turn. My webpage can generate a calendar with a spare color colu ...

`The process of adding an element to the beginning of an array``

I find myself in this scenario: I am dealing with 2 array1 variables, defined as array1 = ["fruit","vegetables"]; and array2 = [["apple","banana"],["tomato"]]; // index 0:represent fruit i,e (["apple","banana"]), index 1: vegetables i,e (["tomato"]) T ...

a guide to incorporating Google Maps into your website using latitude and longitude coordinates

After retrieving a list of latitudes and longitudes from my API for an AngularJS application, I want to display a Google map indicating the positions of these coordinates. I attempted using an iFrame but it only displayed a blank page since the latitudes ...

Extending Angular 2 functionality from a parent component

As a continuation of the discussion on Angular2 and class inheritance support here on SO, I have a question: Check out my plunckr example: http://plnkr.co/edit/ihdAJuUcyOj5Ze93BwIQ?p=preview Here is what I am attempting to achieve: I want to implement s ...

Modifying computed object in Vue: A step-by-step guide

My issue involves a simple selection of months: <select v-model="month" v-on:change="dateChanged('month', month)"> <option v-for="(month, index) in months" :value="index">{{ month }}</option> </select> The primary da ...

Creating a file structure for JavaScript files in a Vue CLI project

When structuring my Vue CLI project, I'm struggling to find clear documentation on best practices. Currently, I have 10 modules each with an associated JS file. My approach so far involves organizing all the pages in my router.js within a views direc ...

What is preventing me from having two consecutive waits in a row?

So I've been having trouble with clicking a checkbox using the correct xpath. It only seems to work when the checkbox is visible after scrolling down the page. I came across some javascript code called scrollviewandclick that is used in conjunction wi ...

Generating a unique image switch with disabled hover functions

My latest project involves coding a block that displays an image on an HTML page, with a new image appearing each time the user hovers over it. However, there's a glitch in my code that causes the images to flicker erratically if the mouse is moved ev ...

Looping through ng-repeats, extracting checked checkbox values in Angular

I am currently dealing with multiple nested ng-repeats in my project, and the third level down consists of a group of checkboxes. Initially, I receive an array of options for these checkboxes, which I handle with the following code snippet: <div class= ...

Having trouble displaying specific images on React Native, how can I resolve this issue?

I am currently developing a weather application that retrieves weather information and displays it using ForecastItem components. However, I have noticed that some of the components do not display the weather image randomly. On the Home screen, I use the ...

The Intl.NumberFormat function does not support conversion to the pt-BR (Brazilian Portuguese) locale

A code sample is provided below: const formCurrency = new Intl.NumberFormat('pt-BR', { style: 'currency', currency: 'BRL', minimumFractionDigits: 2 }) Assuming the input is: var money = 1000.50 formCurrency.fo ...

Unable to update npm packages from package.json file

My Angular .NET core website was initially built using version 4.2.5, but I now want to upgrade it to version 5.2.7 or a newer release. To reflect this change, I have updated the package.json file as follows: { "name": "JobWeb", "private": true, "ve ...

Using jquery to perform ajax queries in sequence

After trying multiple versions of code, I've been unable to figure this out. As someone coming from a php background, JavaScript is new to me. Let's say we have an array with three patches: patch1, patch2, patch3. What I want to achieve: Make ...

Node.JS Plugin Management - Enhance Your Application with Customized

Currently, I am deeply involved in the development of a complex application using Node.JS, built on top of Express. I decided to implement a flexible plugin system to make things easily plug-and-play. The structure of this system consists of: root/ | p ...

Tips for displaying the initial slide when a tab is loaded on a multi-tab webpage

I have a total of four tabs, with the first tab containing a slideshow. On page load, I am successfully triggering the first tab, but for some reason, the first slide in the slideshow within that tab is displaying a blank image. I'm struggling to find ...

Firestore - Using arrayUnion() to Add Arrays

Is there a way to correctly pass an array to the firebase firestore arrayUnion() function? I encountered an issue while attempting to pass an array and received the following error message: Error Error: 3 INVALID_ARGUMENT: Cannot convert an array value ...

Issue with Tanstack React Query failing to import

Attempting to integrate tanstack react query into my current project to handle state management has proven challenging. Following a tutorial on using react query with nextjs 13, I thought I was following it correctly. However, I encountered various import ...

Is there a way to calculate the number of days between two selected dates using the bootstrap date range picker?

I recently implemented the bootstrap daterange picker for selecting start and end dates, and it's been working perfectly. However, I now have a requirement to display the count of days selected. Here's my current code: $('input[name="datera ...

Possible revision: "Exploring ways to iterate through an array of objects in Node/Express and verify if a corresponding entry exists in a MongoDB

Working on building a web application using the MEAN stack and Yelp API to retrieve an array of objects representing local businesses. I manipulate this data on the front-end, but before sending a response, I need to verify if a specific object exists in m ...