简体   繁体   English

HTTP请求后未触发NgOnChanges

[英]NgOnChanges Not Firing After HTTP Request

I have an issue with ngonchanges not firing. 我有ngonchanges无法触发的问题。

I have this component: 我有这个组成部分:

@Component({
    selector: 'conversation-detail',
    templateUrl: './conversation-detail.component.html'
})
export class ConversationDetailComponent implements OnChanges{
    @Input()
    selectedConversation: Conversation;

    title = 'Conversation Detail';    
    convoMessages: Array<Message> = [];


    constructor(
            private _messageService: MessageService
    ){};

    ngOnChanges(changes: SimpleChanges){        
        this.getMessages();
    }

    ngOnInit(): void{

    } 

    private getMessages(){
        if(this.selectedConversation != undefined){
            this._messageService.getMessagesForConversation(this.selectedConversation.ConversationId).then(
                data => {
                    if (data) {
                        this.convoMessages = data.serviceResult as Array<Message>;
                    } else {
                        //This really should never happen
                        console.error('Error retrieving users data: Data object is empty');
                    }
                },
                error => {
                    //loader.dismiss();
                    console.error('Error retrieving users data');
                    console.dir(error);
                }
            );
        }
    }    

}

The ngonchanges will fire the first time selectedConversation changes. ngonchanges将在selectedConversation第一次更改时触发。 GetMessages is called and it loads in the message data. 调用GetMessages并加载消息数据。 After that ngonchanges no longer fires when selectedConversation changes. 之后,当selectedConversation更改时,ngonchanges将不再触发。

If I comment out the function call to getMessages then ngonchanges will fire every time selectedConversation changes. 如果我注释掉对getMessages的函数调用,则每次selectedConversation更改时,ngonchanges都会触发。

so, I'm not sure what is going on in getMessages that cuts off ngonchanges from firing off. 因此,我不确定getMessages中发生了什么,该异常会阻止ngonchanges触发。 The request returns data and the call ends. 该请求返回数据,并且调用结束。 Anyone have any ideas? 有人有想法么?

ngOnChanges only fire if @Input change reference , for example Input type of string , boolean , number , immutable object. ngOnChanges仅在@Input更改reference (例如, stringbooleannumber ,不可变对象的输入类型)时触发。

assume [selectedConversation] bind to an array arrSelected , if you do something like arrSelected.push , the array does not change ref so ngOnChanges won't fire. 假设[selectedConversation]绑定到数组arrSelected ,如果您执行arrSelected.push之类的arrSelected.push ,则该数组不会更改ref因此ngOnChanges不会触发。

so you need using immutable array, using concat , slice , etc to change the array reference 因此,您需要使用不可变数组,使用concatslice等来更改数组引用

Please change your component as : 请更改您的组件为:

@Component({
    selector: 'conversation-detail',
    templateUrl: './conversation-detail.component.html',
    changeDetection: ChangeDetectionStrategy.Default
})

This works fine for me. 这对我来说很好。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM