簡體   English   中英

Angular 2如何防止特定變量的數據綁定

[英]Angular 2 how to prevent data binding for specific variable

因此,我需要防止特定變量的數據綁定。 我想這樣做:

// data is mostly an object in my case.
// it would be nice if there's a global solution
function(data) {
    d = data; // variable that changes on user input
    oldD = data; // variable that shouldn't change on user input
}

但是每當我實現這樣的代碼時,d變量更改時oldD變量就會更改。 我想防止這種情況的發生。 但是,如何防止這種情況發生?

您需要分配值而不分配舊對象的引用。

這是JavaScript / Angular的解決方案。

let oldD = Object.assign({}, data);

希望這可以幫助。

可能您正在尋找如何克隆對象。

function(data) {
    d = data; // variable that changes on user input  

    // creates brand new object with the same data
    let oldD = Object.create(data.constructor.prototype);
    data.constructor.apply(oldD);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM