1

To learn Angular I'm coding a to-do list. I've created a component task with a title (task name) and status (a boolean related to a checkbox input) parameters.

<div>
  <div>{{title}} <button>Edit</button></div>
  <input type="checkbox" value={{status}}>
</div>

I've also created a ToDoList component which contain a title(name of the to-do list, and a tab with a list of task name to generate multiple task component using a *ngFor.

<div>
  <h1>{{title}}</h1>
  <app-task *ngFor="let title of list" [title]="title" status="true"></app-task>
  <button (click)="appTaskDirective">Add a task</button>
</div>

Now I want to add dynamically task components on click on the "Add a task button" while using the appTaskDirective but I just can't figure it out.

The angular document example can't help.

Here is my appTaskDirective file:

import { Directive, ViewContainerRef, TemplateRef, OnInit } from '@angular/core';

@Directive({
  selector: '[appTaskDirective]'
})

export class TaskDirectiveDirective implements OnInit {
  constructor(public viewContainer:ViewContainerRef) { }
}

Thanks.

5
  • 1
    check this: stackoverflow.com/a/54027187/9386929 Commented Apr 12, 2019 at 13:27
  • 1
    this may help too: stackoverflow.com/a/53191913/9386929 Commented Apr 12, 2019 at 13:28
  • 1
    Possible duplicate of Angular 5: Lazy-loading of component without routing Commented Apr 12, 2019 at 13:28
  • I think your a bit confused about how attributes directives work. To accomplish your purpose you don't need a directive, but just a method that push another title inside the list array and that is called on button's click Commented Apr 12, 2019 at 13:51
  • I've created this stackblitz to show you how directives work. But @Yusuff answer, explains exactly what I meant in my last comment. P.S. your ToDoList component it's the app.component in my code: stackblitz.com/edit/… Commented Apr 12, 2019 at 14:15

1 Answer 1

1

first of all you can't pass a directive to the click event. If you want to use the directive to listen to the click event you have to attach an hostlistener to the directive like this

In directive

import { Directive, ViewContainerRef, TemplateRef, OnInitHostListener } from '@angular/core';

@Directive({
  selector: '[appTaskDirective]'
})

export class TaskDirectiveDirective implements OnInit {
  constructor(public viewContainer:ViewContainerRef) { }
    @HostListener('click') onClick() {
     // do something
    }
}

In component

<div>
  <h1>{{title}}</h1>
  <app-task *ngFor="let title of list" [title]="title" status="true"></app-task>
  <button appTaskDirective>Add a task</button>
</div>

But I don't see why you need to do all these though. You can just listen to the event in the component and update the tasklist accordingly

In component html

<div>
 <h1>{{title}}</h1>
 <app-task *ngFor="let title of list" [title]="title" status="true"></app-task>
 <button (click)="addNewTask()">Add a task</button>
</div>

In component ts

addNewTask() {
   this.list.push('A new Task');
}

Hope this helps :)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.