zoukankan      html  css  js  c++  java
  • angular2 2种方式----获取子组件的类属性和类方法

    1)-------方法
    -----父html文件
    <button (click)="onclickchildfun()">通过@ViewChild调用子组件方法</button>
    ----父ts
    import { Component, OnInit, ViewChild } from '@angular/core';
    import { childComponent } from './childView/child.component';
    @Component({
    selector: 'logoSing',
    templateUrl: './logoSing.html',
    styleUrls: ['./logoSing.css']
    })
    export class LogoSingComponent implements OnInit {
    @ViewChild(childComponent) child: childComponent;//这里传入一个子组件实例引入,定义了一个变量child接收
    onclickchildfun(){//调用子组件方法
    this.child.func()
    }
    }
    -----子ts
    import {Component, Output, EventEmitter,OnInit} from '@angular/core';
    @Component({
    selector: 'child-Component',
    templateUrl: './child.component.html',
    styleUrls: ['./child.component.less']
    })

    export class childComponent implements OnInit {
    func(){
    console.log("ViewChild ----- 子组件的打印值")
    }

    }

    2)-----方法

    ----父html文件
    <button (click)="child.func()">通过#号调用子组件方法</button>
    <child-Component #child></child-Component>//通过#号来标识一个变量, 这样在父组件模板中创建了一个局部变量#chiden来获取子组件的实例,
    调用子组件中的方法和属性

    ----父ts
    import { Component, OnInit, ViewChild } from '@angular/core';
    import { childComponent } from './childView/child.component';
    @Component({
    selector: 'logoSing',
    templateUrl: './logoSing.html',
    styleUrls: ['./logoSing.css']
    })
    export class LogoSingComponent implements OnInit {

    }
    -----子ts
    import {Component, Output, EventEmitter,OnInit} from '@angular/core';
    @Component({
    selector: 'child-Component',
    templateUrl: './child.component.html',
    styleUrls: ['./child.component.less']
    })

    export class childComponent implements OnInit {
    func(){
    console.log("# ----- 子组件的打印值")
    }

    }

  • 相关阅读:
    机器学习作业(八)异常检测与推荐系统——Matlab实现
    机器学习笔记(九)异常检测与推荐系统
    Coursera 吴恩达 机器学习 学习笔记
    机器学习作业(七)非监督学习——Matlab实现
    机器学习笔记(八)非监督学习

    希尔排序
    霍纳算法的散列函数
    javascript判断给定字符串是否是回文
    JavaScript链表
  • 原文地址:https://www.cnblogs.com/lihong-123/p/10006014.html
Copyright © 2011-2022 走看看