简体   繁体   English

使用@Input装饰器从角度组件外部传递数据

[英]Using @Input decorator to pass data from outside the angular component

In an ionic app, i am using a angular component. 在离子应用程序中,我使用角度组件。 In that angular component, i have a variable headerText which in initialized from the page where this component is used. 在该角度组件中,我有一个变量headerText ,它从使用该组件的页面初始化。

Problem is that headerText variable is always undefined. 问题是headerText变量总是未定义的。

How can i fix this problem? 我该如何解决这个问题?

This is the angular component where headerText variable is defined. 这是定义headerText变量的角度组件。

import { Component, Input, OnInit } from '@angular/core';
import { MenuController, NavController } from 'ionic-angular';

@Component({
  selector: 'custom-header-text',
  templateUrl: 'custom-header-text.html'
})
export class CustomHeaderTextComponent implements OnInit {

  @Input() headerText: string;

  constructor(private navCtrl :  NavController,
              private menuCtrl : MenuController) {

  }

  ngOnInit() {
    setTimeout(() => {
      console.log('text = ' + this.headerText);
    }, 3000);
  }

  goBack() {
    this.navCtrl.pop();
  }

  openMenuPage() {
    this.menuCtrl.enable(true,'bl-menu')
    this.menuCtrl.open();
  }
}

This is how i am passing a value to this headerText variable from where this component is used. 这是我如何将值传递给此headerText变量使用此组件的位置。

<custom-header-text [headerText]="'Inbox'"></custom-header-text>

See below. 见下文。

The reason why this is not working is that your root element in which you place the <custom-header-text [headerText]="'Inbox'"></custom-header-text> is not an angular component. 这不起作用的原因是您放置<custom-header-text [headerText]="'Inbox'"></custom-header-text>的根元素不是角度组件。 Because of this, Angular won't compile this element. 因此,Angular不会编译此元素。 And Angular does not read attribute values during runtime, only during compile time, as otherwise we would get a performance hit. Angular不会在运行时读取属性值,只是在编译期间,否则我们会受到性能影响。

Answered here 这里回答

Angular 2 input parameters on root directive 根指令上的Angular 2输入参数

Your code wasn't working as the HeaderModule was not correctly configured. 您的代码无法正常工作,因为未正确配置HeaderModule。

There were a few issues with your implementation: 您的实施存在一些问题:

  1. The path to the Header Module wasn't correctly set in AppModule. 未在AppModule中正确设置Header模块的路径。
  2. The path to the HeaderComponent template was also not set to the correct HTML file. HeaderComponent模板的路径也未设置为正确的HTML文件。

After fixing those, it was working as expected. 修好后,它按预期工作。 Here's an Updated StackBlitz for your ref. 这是你的ref的更新StackBlitz

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用角度8中的@input装饰器将数据从父组件传递到子组件 - Pass data from parent component to child component using @input decorator in angular 8 如何在Ionic2中不使用@input装饰器将数据从父组件传递到子组件(或引发事件) - how to pass data (or raise a event) from parent component to child component without using @input decorator in Ionic2 尝试使用输入装饰器将对象从 api 传递给子组件 - Trying to pass object from api to child component using Input Decorator 使用 Input 组件的 Angular Pass 数据 - Angular Pass data using Input component 如何将 Typescript 类型“传递”到 Angular 组件 @Input 装饰器 - How to "pass down" Typescript type to Angular component @Input decorator 有没有办法使用@Input 装饰器将数据从一个组件共享到不同模块中的另一个组件 - Is There a way to share data from one component to another component which are in different modules by using @Input decorator 使用带有路由器的输入装饰器将值从父组件传递给子组件 - pass the value from parent component to child with input decorator with router 如何使用属性绑定将数据传递给 angular html 组件中的输入 - How to pass data to the input in angular html component using property binding 如何使用 angular 14 中的每个方法和输入装饰器传递下拉列表的值 - How to pass the value for the dropdown from using each method and input decorator in angular 14 如何将数据 Input() 从组件传递到服务 class Angular - how to pass data Input() from component to service class Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM