2017-08-17 62 views
0

我創建一個即時通訊軟件。我的功能cliked(線程)允許我點擊討論並查看討論。用戶不能同時打開2個以上的聊天窗口。 所以當兩個聊天窗口打開時(this.threadService.windows.length === 2),我必須關閉第一個窗口才能顯示新窗口。保存功能中的舊參數

我必須保存這個第一個窗口中的變量,才能進入它作爲我的函數的參數closeOldThread(oldThread)

clicked(thread: Thread): void { 
this.newWindow = true; 

let oldThread: Thread; 
if(this.threadService.windows.length === 2) { 
    this.closeOldThread(oldThread); 
} 
if (!this.thread.isOpen) { 
    thread = this.chatService.openThread(thread); 
    this.chatService.setCurrentThread(thread); 
    this.thread.isOpen = true; 
} 
oldThread = thread; 
} 

////

closeOldThread(thread: Thread): void { 
let index = this.threadService.windows.indexOf(thread); 
this.threadService.windows.splice(index, 1); 
this.thread.isOpen = false; 
this.newUser = false; 
this.newWindow = false; 
} 

那可能嗎? 因爲在我的情況下,oldThread是未定義的。

回答

1
clicked(thread: Thread): void { 
    this.newWindow = true; 
    if(this.threadService.windows.length === 2) { 
     // get the previously opened chat window 
     let oldThread = this.threadService.windows[0];    
     this.closeOldThread(oldThread); 
    } 
    if (!this.thread.isOpen) { 
     thread = this.chatService.openThread(thread); 
     this.chatService.setCurrentThread(thread); 
     this.thread.isOpen = true; 
    } 
} 
+0

它也行不通。 當我測試我的變量(_this.oldThread_)在我的if循環(_this.threadService.windows.length === 2_)它等於'undefined'.. – Floriane

+0

什麼是存儲在'this.threadService.windows'? Thread類型的對象? – Faisal

+0

沒有線程數組:'windows:Thread [];' – Floriane