我有一個臨時JavaScript對象currentExpense
,對象構造TestExpense
和陣列TestExpenses
的角度控制裝置,其新創建的對象TestExpense
將通過功能submitExpenses
推動。這些代碼如下:NG-模型JavaScript對象屬性設置爲陣列
$scope.currentExpense = {
employeeId: 9,
date: '',
account: '',
task: '',
expenseType: '',
expenseCategory: '',
notes: '',
amount: ''
};
function TestExpense(employeeId, date, account, task, expenseType,
expenseCategory, notes, amount) {
this.employeeId = employeeId;
this.date = date;
this.account = account;
this.task = task;
this.expenseType = expenseType;
this.expenseCategory = expenseCategory;
this.notes = notes;
this.amount = amount;
}
$scope.TestExpenses = [];
$scope.submitExpenses = function() {
$scope.TestExpenses = [];
$scope.TestExpenses.push(
new TestExpense(
$scope.currentExpense.employeeId,
$scope.currentExpense.date,
$scope.currentExpense.account,
$scope.currentExpense.task,
$scope.currentExpense.expenseType,
$scope.currentExpense.expenseCategory,
$scope.currentExpense.notes,
$scope.currentExpense.amount
)
);
console.log($scope.TestExpenses);
};
一個在我看來的ng-model
用法如下:
<select multiple="" class="form-control" ng-model="currentExpense.expenseType"
ng-options="task.expenseTypeId as task.expenseTypeTitle for task in ExpenseTypes" required>
</select>
這裏是相關ExpenseType
構造函數和ExpenseTypes
陣列:
function ExpenseType(expenseTypeId, expenseTypeTitle) {
this.expenseTypeId = expenseTypeId;
this.expenseTypeTitle = expenseTypeTitle;
}
$scope.ExpenseTypes = [];
我的問題是,在記錄TestExpenses
數組中的對象時,我看到數組爲expenseType
pro perty:
TestExpense
account:"a"
amount:""
date:""
employeeId:9
expenseCategory: Array[1]
0: 2
length:1
__proto__:Array[0]
expenseType:Array[1]
0:4
length:1
__proto__:Array[0]
notes:""
我的目標是讓expenseType
財產僅僅是4
,而不是它目前被設置爲數組。
任何有關如何完成此操作的建議都非常受歡迎!
任何Plunkr或到的jsfiddle看到它在行動? – malix