加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular4 @Input()将值从父组件传递到子组件

发布时间:2020-12-17 10:24:51 所属栏目:安全 来源:网络整理
导读:我在App component.html中有一个像这样的代码. form input #box1 (blur)="onKey1(box1.value)" type="text" name="username" p{{box1.value}}/p /form 在AppComponent.ts中我有这样的代码. import { Component } from '@angular/core';import { OnInit } fro
我在App component.html中有一个像这样的代码.
<form>
        <input #box1 (blur)="onKey1(box1.value)" type="text" name="username">
        <p>{{box1.value}}</p>
    </form>

在AppComponent.ts中我有这样的代码.

import { Component } from '@angular/core';
import { OnInit } from '@angular/core/src/metadata/lifecycle_hooks';
@Component({
    selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.css']
})
export class AppComponent{
    type:string;
    constructor() { }
    onKey1(value:string)
    {
        this.type=value;  
    }
}

现在我创建了一个名为MyComponent3的组件.在该组件中,我想从app组件中检索数据.MyComponent3.html的代码如下:

<p>{{type}}</p>

在MyComponent3.ts中,我有以下代码.

import { Component,OnInit,ViewEncapsulation,Input } from '@angular/core';
@Component({
    selector: 'app-my-component3',templateUrl: './my-component3.component.html',styleUrls: ['./my-component3.component.css'],encapsulation: ViewEncapsulation.None
})
export class MyComponent3Component implements OnInit {
    @Input() type;
    ngOnInit() {
    }
}

但是在输出中,值没有从AppComponent传递到My Component3.

应该在其中提及您要传递给子组件的任何属性.您可以使用@input和html传递给子组件,如下所示,
<app-my-component3 [type]="type"></app-my-component3>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读