父html怎么调用子html方法,如何从父组件调用html事件方法到子组件

我有以下父组件。

JS

import { Component, OnInit } from '@angular/core';

import { TaskService } from '../task.service';

import { ViewChild } from '@angular/core/src/metadata/di';

import { CreateNewTaskComponent } from '../create-new-task/create-new-task.component';

@Component({

selector: 'task-list',

templateUrl: './task-list.component.html',

styleUrls: ['./task-list.component.css']

})

export class TaskListComponent implements OnInit {

tasks = [{}];

constructor(public taskService: TaskService) {

debugger;

this.tasks = taskService.tasks;

}

ngOnInit() {

}

completeTask(task){

task.completed = true;

}

}

HTML

Completed Task List

Task TitleTask StatusActions
No Record To Display
{{task.title}}{{task.completed ? 'Completed' : 'Pending'}}

Complete

Edit

上面是从父组件到子组件的事件绑定调用editTask()。

以下是子组件

JS

import { Component, OnInit } from '@angular/core';

import { TaskService } from '../task.service';

@Component({

selector: 'create-new-task',

templateUrl: './create-new-task.component.html',

styleUrls: ['./create-new-task.component.css']

})

export class CreateNewTaskComponent implements OnInit {

task;

constructor(public taskService: TaskService) {

this.task = {id:0, title: '', completed: false};

}

ngOnInit() {

}

createTask(){

debugger;

this.task.completed = false;

this.task.id = this.taskService.tasks.length;

this.taskService.tasks.push(this.task);

this.task = {id:0, title: '', completed: false};

}

editTask(task){

alert("Called");

}

}

HTML

Create New Task


Save

现在,当我点击父组件编辑按钮时,我收到以下错误:

错误TypeError:Object不支持属性或方法'editTask'

请帮忙!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值