Angular 子组件ngOnChanges中的演示变量不起作用
在child.component.ts中。我正在将demo变量与input()一起使用。它不会显示在开发人员控制台中 app.htmlAngular 子组件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
<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);
}
}