我正在尝试unshift
现有数组列表的对象。但是当我这样做时,我得到的值之一是empty
。在angularjs
中将新对象推送到现有数组的正确方法是什么?
这是我的代码:
var staticPhase = {
"DisciplineId": "0",
"DisciplineName": "All",
"PhaseId": "0",
"PhaseName": "All" // but this is being converted as ''
}
if($scope.firstLoad) {
var newList = angular.copy( $scope.project.ProjectSummaryFilter ); //taking a copy of existing array
var filterById = $filter('filter')(newList, { ProjectId : $scope.projectId })[0];
staticPhase.ProjectId = filterById.ProjectId;
staticPhase.ProjectName = filterById.ProjectName;
staticPhase.SubProjectId = filterById.SubProjectId;
staticPhase.SubProjectName = filterById.SubProjectName;
}
var phaseList = $scope.project.ProjectSummaryFilter;
phaseList.unshift(staticPhase) //adding static phase to array;
答案 0 :(得分:1)
标准JavaScript push()
方法应该有效:
var phaseList = $scope.project.ProjectSummaryFilter;
phaseList.push(staticPhase);