2016-11-11 73 views
-1

我期待延長一個動態對象與其他的方式。在angularjs,怎麼一個對象複製到另一個?

比方說,我們有兩個目標:

person = { 
    FirstName: "John", 
    LastName: "Doe", 
    IsStudent: false }; 

student = { 
    University: "MIT", 
    Faculty: "Computers", 
    IsStudent: true } 

是否有可能學生對象複製到人對象,以便我能得到這個結構的擴展Person對象:

person = { 
    FirstName: "John", 
    LastName: "Doe", 
    University: "MIT", 
    Faculty: "Computers", 
    IsStudent: true }; 

基本上這個想法是更新的結構和人物對象的狀態,包括從學生對象的學生信息:

  • 人說不要在學生中存在性能保持
  • 人的存在於學生的屬性將被覆蓋(如IsStudent屬性)
  • 學生屬性添加到人從而創造和人身

重要加長版:不能手動複製財產的屬性。

謝謝。

回答

1

我猜你正在尋找extend。見documentation

angular.extend(student, person); 
0

爲了您提到的功能,您可以在JS中單獨使用以及使用ES6。

這裏是Object.assign

let person = { 
 
    FirstName: "John", 
 
    LastName: "Doe", 
 
    IsStudent: false 
 
}; 
 

 
let student = { 
 
    University: "MIT", 
 
    Faculty: "Computers", 
 
    IsStudent: true 
 
}; 
 

 
console.log(Object.assign(person, student)); 
 
console.log(angular.extend(person, student));
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

爲ES6的文檔
相關問題