我有以下對象數組:如何在AngularJS中創建一個類似於對象數組對象的空對象?
$scope.users = [
{
ID: "1",
Name: "Hege",
Username: "Pege",
Password: "hp",
},
{
ID: "2",
Name: "Peter",
Username: "Pan",
Password: "pp"
}
];
我需要創建與這樣的空值的類似對象,
$scope.newUser = {
ID: "",
Name: "",
Username: "",
Password: ""
}
,這樣我可以將其推到相同的陣列($scope.users.push($scope.newUser);
) ,讓它看起來像這樣:
$scope.users = [
{
ID: "1",
Name: "Hege",
Username: "Pege",
Password: "hp"
},
{
ID: "2",
Name: "Peter",
Username: "Pan",
Password: "pp"
},
{
ID: "",
Name: "",
Username: "",
Password: ""
}
];
然而,陣列$scope.users
不會總是有相同的陣列對象。我需要它,即使我的數組更改爲不同的東西,例如,像這樣的工作:
$scope.users = [
{
SID: "pepe",
Name: "Peter",
School: "Primary School"
},
{
SID: "hepe",
Name: "Hege",
School: "Junior School"
}
];
我怎樣才能做到這一點?
你確定在陣列將與同一只對象結構體?反正angular不提供清理對象字段的方法,但是你可以簡單的寫你自己的函數 – Grundy
你爲什麼要這樣做?我知道這不是一個答案,但它似乎違反了我所說的好的編程。有一組東西包含未定義的東西聞起來。 –