Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.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 子组件ngOnChanges中的演示变量不起作用_Angular_Angular Material_Angular Ui Router - Fatal编程技术网

Angular 子组件ngOnChanges中的演示变量不起作用

Angular 子组件ngOnChanges中的演示变量不起作用,angular,angular-material,angular-ui-router,Angular,Angular Material,Angular Ui Router,在child.component.ts中。我正在将demo变量与input()一起使用。它不会显示在开发人员控制台中 app.html <input type="text" placeholder="text" #val> <br> <button (click)="submitValue(val)">submit</button> <app-child [myValue]="value"></app-child> chi

在child.component.ts中。我正在将demo变量与input()一起使用。它不会显示在开发人员控制台中

app.html

<input type="text" placeholder="text" #val>
<br>
<button (click)="submitValue(val)">submit</button>
<app-child [myValue]="value"></app-child>
child.html

<p>from child...!</p>
<strong>{{myValue}}</strong>
在Chrome开发者控制台中,我看到:

{myValue: SimpleChange}
myValue: SimpleChange {previousValue: undefined, currentValue: undefined, firstChange: true}
__proto__:
constructor: ƒ Object()
__defineGetter__: ƒ __defineGetter__()
__defineSetter__: ƒ __defineSetter__()
hasOwnProperty: ƒ hasOwnProperty()
__lookupGetter__: ƒ __lookupGetter__()
__lookupSetter__: ƒ __lookupSetter__()
isPrototypeOf: ƒ isPrototypeOf()
propertyIsEnumerable: ƒ propertyIsEnumerable()
toString: ƒ ()
valueOf: ƒ valueOf()
toLocaleString: ƒ toLocaleString()
get __proto__: ƒ __proto__()
set __proto__: ƒ __proto__()
使用input()演示变量。它不会显示在开发人员控制台中

只显示我的价值

使用input()演示变量。它不会显示在开发人员控制台中

是,因为您没有将其传递给
子组件

您当前的代码:

<app-child [myValue]="value"></app-child>

它正在按预期工作。最初,您发送的是
value
变量,而没有为其赋值。因此,它正在发送
未定义的
。注意
myValue:SimpleChange{previousValue:undefined,currentValue:undefined,firstChange:true}
previousValue
currentValue
都是
未定义的
,因为您发送了未定义的

要查看更改,请在输入框中键入一个值,然后单击提交。现在,新值应该在子组件的
changes['myValue']中可用。currentValue

是对你的代码的堆栈闪电战。我什么都没变

{myValue: SimpleChange}
myValue: SimpleChange {previousValue: undefined, currentValue: undefined, firstChange: true}
__proto__:
constructor: ƒ Object()
__defineGetter__: ƒ __defineGetter__()
__defineSetter__: ƒ __defineSetter__()
hasOwnProperty: ƒ hasOwnProperty()
__lookupGetter__: ƒ __lookupGetter__()
__lookupSetter__: ƒ __lookupSetter__()
isPrototypeOf: ƒ isPrototypeOf()
propertyIsEnumerable: ƒ propertyIsEnumerable()
toString: ƒ ()
valueOf: ƒ valueOf()
toLocaleString: ƒ toLocaleString()
get __proto__: ƒ __proto__()
set __proto__: ƒ __proto__()
<app-child [myValue]="value"></app-child>
<app-child [myValue]="value" [demo]="someAmazingText"></app-child>
ngOnChanges(changes: SimpleChange }) {
  if(changes['myValue'] && changes['myValue'].previousValue != changes['myValue'].currentValue)
  {
    console.log('The latest value', changes['myValue'].currentValue);
  }
}