Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度:页面和组件之间的数据绑定,不适用于ionic 4_Angular_Ionic Framework_Data Binding_Ionic4_Angular Components - Fatal编程技术网

Angular 角度:页面和组件之间的数据绑定,不适用于ionic 4

Angular 角度:页面和组件之间的数据绑定,不适用于ionic 4,angular,ionic-framework,data-binding,ionic4,angular-components,Angular,Ionic Framework,Data Binding,Ionic4,Angular Components,我无法使用数据绑定将数据从页面传递到组件 当我试图在构造函数中记录该值时,它表示该值未定义。message是用于实现数据绑定的变量的名称 app-floating-text-white.ts(组件) import { Component, OnInit ,Input} from '@angular/core'; @Component({ selector: 'app-floating-text-white', templateUrl: './floating-text-white.co

我无法使用数据绑定将数据从页面传递到组件

当我试图在构造函数中记录该值时,它表示该值未定义。message是用于实现数据绑定的变量的名称

app-floating-text-white.ts(组件)

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

@Component({
  selector: 'app-floating-text-white',
  templateUrl: './floating-text-white.component.html',
  styleUrls: ['./floating-text-white.component.scss'],
})
export class FloatingTextWhiteComponent implements OnInit {

 @Input() message: string;

 constructor() { 
   console.log(this.message)
 }

 ngOnInit() {}

}
<p> {{message}} </p>
app-floating-text-white.html(组件)

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

@Component({
  selector: 'app-floating-text-white',
  templateUrl: './floating-text-white.component.html',
  styleUrls: ['./floating-text-white.component.scss'],
})
export class FloatingTextWhiteComponent implements OnInit {

 @Input() message: string;

 constructor() { 
   console.log(this.message)
 }

 ngOnInit() {}

}
<p> {{message}} </p>
{{message}

page.html

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

@Component({
  selector: 'app-floating-text-white',
  templateUrl: './floating-text-white.component.html',
  styleUrls: ['./floating-text-white.component.scss'],
})
export class FloatingTextWhiteComponent implements OnInit {

 @Input() message: string;

 constructor() { 
   console.log(this.message)
 }

 ngOnInit() {}

}
<p> {{message}} </p>
<app-floating-text-white [message]="Hello World"> </app-floating-text-white>

这是预期的行为。在app-floating-text-white.ts中,消息变量在绑定“生效”之前是未定义的。因此,当调用console.log时,构造函数(发生在组件创建时)的值仍然是未定义的

在最近的CDR周期中,绑定将被刷新,新值“Hello World”将被传递到您的子组件中

尝试添加ngAfterViewInit{console.log(this.message)},看看刷新所有绑定后会发生什么(检查并刷新所有绑定后会触发ngAfterViewInit)

我还注意到,您在这里使用单引号来绑定值:

<app-floating-text-white [message]="Hello World"> </app-floating-text-white>

上面的代码期望helloworld是一个变量。您需要将其设置为字符串:

<app-floating-text-white [message]="’Hello World’"> </app-floating-text-white>


或者绑定到页面的实际变量。ts

我在ngAfterViewInit中尝试了{console.log(this.message)},但仍然得到未定义的值。您可以尝试使用以下方法代替它:[message]=“Hello World”do[message]=“Hello World”-基本上将Hello World添加到其他引号中吗?