question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generating then trying to use a simple directive isn;t working in Ionic 4

See original GitHub issue

Bug Report

Ionic Info Run ionic info from a terminal/cmd prompt and paste the output below.


Ionic:

   ionic (Ionic CLI)          : 4.1.1 (/home/nmartin/npm/lib/node_modules/ionic)
   Ionic Framework            : @ionic/angular 4.0.0-beta.3
   @angular-devkit/core       : 0.7.4
   @angular-devkit/schematics : 0.7.4
   @angular/cli               : 6.1.4
   @ionic/ng-toolkit          : 1.0.7
   @ionic/schematics-angular  : 1.0.5

System:

   NodeJS : v10.6.0 (/usr/local/bin/node)
   npm    : 6.3.0
   OS     : Linux 4.18

Describe the Bug custom attribute Directives are not working in Ionic 4

Steps to Reproduce Steps to reproduce the behavior:

  1. run ionic start myApp tabs --type=angular
  2. run ionic g directive hilite
  3. edit hilite.directive.ts to look like:
import { Directive, ElementRef } from '@angular/core';

@Directive({
  selector: '[appHilite]',
})
export class HiliteDirective {
  constructor(el: ElementRef) {
    console.log('hilite!!!');
    el.nativeElement.style.backgroundColor = 'yellow';
  }
}

  1. edit app/home/homt.page.html to look like:
<p appHilite>If you get lost, the <a target="_blank" rel="noopener" href="https://ionicframework.com/docs">docs</a>
    will be your guide.</p>
  1. run the app

Expected Behavior the paragraph to have a yellow background

Actual Behavior Nothing. Interestingly, If I move the directive injection from app.module.ts to home.module.ts, it works, BUT only in home.page.html, not any other page in the app, due to duplicate declarations

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
keywirecommented, Sep 25, 2018

I have also seen this - it seems that a custom directive will work if declared on a page which uses it directly or imported from a module on a page directly but NOT when it is declared in app.module.ts or imported as a module into app.module.ts … 😦 …

Current workaround is to put the directives into a common module and import that module into the pages where the directive is required. Not ideal but at least something.

0reactions
ionitron-bot[bot]commented, Jan 12, 2019

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generating then trying to use a simple directive isn;t working ...
run ionic g directive hilite; edit hilite.directive.ts to look like: import { Directive, ElementRef } from '@angular/core'; @Directive({ ...
Read more >
Directive not working in Ionic 4
Hello, I'm having a problem with directives in Ionic 4. I'm trying to make a simple directive that just outputs something with console.log....
Read more >
Angular 4 / ionic 3 custom attribute directive not working
Hi I am trying to focus on a search input field. I have created a directive for that. It hs been exported from...
Read more >
Using a Directive to Modify the Behaviour of an Ionic Component
In this tutorial, we are going to walk through how to modify the behaviour of the sliding item component in Ionic by creating...
Read more >
Common errors and how to fix them - Mobiscroll Help Center
Runtime errors. 1. An interface may only extend a class or another interface. This error occurs when the Angular/Ionic application's typescript version is ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found