Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.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 8在onchanges事件中修改了一个变量,但当我想要访问它时,我发现它没有被更改_Angular_Input - Fatal编程技术网

angular 8在onchanges事件中修改了一个变量,但当我想要访问它时,我发现它没有被更改

angular 8在onchanges事件中修改了一个变量,但当我想要访问它时,我发现它没有被更改,angular,input,Angular,Input,我在angular中有一个应用程序,我有一个具有custem属性“modeInsert”的组件,我在ngOnChanges()事件中检测到该属性的更改,如下所示: import{Component,OnInit,Input,ViewChild,OnChanges,SimpleChanges}来自“@angular/core”; @组成部分({ 选择器:“应用程序更新cv”, templateUrl:“./update cv.component.html”, 样式URL:['./更新cv.com

我在angular中有一个应用程序,我有一个具有custem属性“modeInsert”的组件,我在ngOnChanges()事件中检测到该属性的更改,如下所示:

import{Component,OnInit,Input,ViewChild,OnChanges,SimpleChanges}来自“@angular/core”;
@组成部分({
选择器:“应用程序更新cv”,
templateUrl:“./update cv.component.html”,
样式URL:['./更新cv.component.css']
})
导出类UpdateCVComponent实现OnInit、OnChanges{
构造函数(){}
模式:布尔型;
恩戈尼尼特(){
}
ngOnChanges(changeRecord:SimpleChanges):无效{
if(typeof changecord.modeInsert!=“未定义”){
this.mode=changecord.modeInsert.currentValue;
console.log(“changecord.\u modeInsert:+this.mode”);
}
}
@输入()模式插入:布尔值;
@ViewChild('closebuttonUpdateCV',{static:false})closebuttonUpdateCV;
onUpdateCV(cv){
console.log(“onUpdateCV mode=“+this.mode”)
如果(本模式){
console.log(“mode=ajout”)
}否则{
console.log(“mode=modif”)
}
this.CloseButtonUpdate.nativeElement.click();
}

}
您将在ngOnChanges中获得当前和以前的值

ngOnChanges(changes: SimpleChanges) {
      for (let propName in changes) {
        let chng = changes[propName];
        let cur  = JSON.stringify(chng.currentValue);
        let prev = JSON.stringify(chng.previousValue);
      }
    }

原因是我放置了这个组件的两个副本,一个在菜单中,另一个在另一个组件中,因此当我从另一个组件调用may Componet时,我会调用菜单上的版本,并且变量this.mode是未定义的。 这是may组件的模板,是模态形式:


&时代;
笔名:
prenom:
民事诉讼:
地址:
电邮:
电话:
费尔默
索夫加德

为什么不在
onUpdateCV
中使用
this.modeInsert
而不是
this.mode
?我看不出将输入变量指定给局部变量有什么意义。谢谢,我没有这样做,因为this.modeInsert除了在onchage中外不会更改其值,那么您的设置还有其他问题。你能提供一个stackblitz来复制这个问题吗。@fridoo当我在defirent place中使用一个组件时,这个问题似乎出现了。我认为,问题不在nfOnChanges()中。它工作得很好。this.mode改变了它的值,但我执行onUpdate CV()this.mode时,它没有被删除为mode添加的额外对象。请使用相同的MODESERT变量,然后重试