How to use Angular structural directive with multiple inputs

JavascriptAngularDirective

Javascript Problem Overview


I want to implement something similar with angular-permisssion. And with requirement to control the element's existance, I need to use angular structural directive.

At the beginning, i think such syntax would work:

<h2 *permissionIf [permissionIfExcept]="'Read'">Except</h2>

However, it doesn't work that way.

Moreover, the offical guide only teach you how to write custom structural directive with single input. With multi-inputs, some third-party tutorials involve a bit. But that's using the angular template micro-syntax to achieve data binding. Then one problem occurs: template syntax doesn't support pure key-value inputs:

<h2 *permissionIf="except: map.except;only: 'test'">Except</h2>

It expands into this(which is illegal):

<h2 template="permissionIf except: map.except;only: 'test'">Except</h2>

A stupid temporary solution is add a useless variable declaration.

<h2 *permissionIf="let i;except: map.except;only: 'test'">Except</h2>

Another inconvenient way is to use template element to wrap the code.

<template permissionIf [permissionIfExcept]="'Read'">
  <h2>Except</h2>
</template>

The above all are not accepetable enough. But I can't find a bette way to resolve it.

Hope some guys can give some suggestion:).

Javascript Solutions


Solution 1 - Javascript

The input names need all to be prefixed with the selector of the directive, followed by the input name capitalized (i.e. permissionIfExcept). Example:

  @Directive({
    selector: '[permissionIf]'
  })
  export class PermissionIfDirective implements AfterContentInit {

    private _permissionIf:string[];
    @Input() 
    set permissionIf(value: string[]) {
      this._permissionIf=value;
      console.log('permissionIf: ', value);
    }
    
    private _except:string;
    @Input() 
    set permissionIfExcept(value: string) {
      this._except = value;
      console.log('except: ', value);
    }
  }

To use them with the '*' syntax:

<div *permissionIf="permissions;except:'Read'"></div>

Note here you're using the name following the prefix uncapitalized (i.e. except). Also note the : in the assignment.

The explicit syntax (using template) would look like this:

<template [permissionIf]="permissions" [permissionIfExcept]="'Read'">
  </div></div>
</template>

but with <ng-container> it could look like

<ng-container *permissionIf="permissions;except:'Read'">
  <div></div>
</ng-container>

Plunker example

See also the source of NgFor as an example.

Solution 2 - Javascript

@Günter Zöchbauer answer is almost correct.

Actually right now to make his answer working you need to explicitly rename the secondary @Input name. So it should be:

@Input("permissionIfExcept") 
set permissionIfExcept(value: string) {
  this._except = value;
  console.log('except: ', value);
}

Solution 3 - Javascript

> Then one problem occurs: template syntax doesn't support pure key-value inputs:

True

> A stupid temporary solution is add a useless variable declaration.

I think you are using this in a way it was not meant to be.

From the docs: > The microsyntax parser title-cases all directives and prefixes them with the directive's attribute name, such as ngFor. For example, the ngFor input properties, of and trackBy, become ngForOf and ngForTrackBy, respectively. That's how the directive learns that the list is heroes and the track-by function is trackById.

microsyntax translation

https://angular.io/guide/structural-directives#microsyntax-examples

Bottom line is in the context of your question, the microsyntax accepts "expression", followed by optional "keyed expression"s and I'm afraid those are your only options.

One could of course pass an object as the first expression—similar to ngIf—, the difference being you can teach your directive how to evaluate the expression:

*permissionIf="{ only: 'whatever', except: ['things', 'stuff'] }"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questione-cloudView Question on Stackoverflow
Solution 1 - JavascriptGünter ZöchbauerView Answer on Stackoverflow
Solution 2 - JavascriptTomasz StelągowskiView Answer on Stackoverflow
Solution 3 - JavascriptJon JaquesView Answer on Stackoverflow