2016-03-02 85 views
0

我正在嘗試unshift一個對象到現有的數組列表。但是當我這樣做,我得到的價值之一是empty。在angularjs中將新對象推送到現有數組的正確方法是什麼?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; 

回答

1

標準JavaScript push()方法應該工作:

var phaseList = $scope.project.ProjectSummaryFilter; 
phaseList.push(staticPhase); 
+0

'unshift'不會工作?我想添加數組作爲列表中的第一個 – 3gwebtrain

+0

仍然我正在使用'push' - 不工作。獲得結果爲空('') – 3gwebtrain

+0

是的,unshift()應該可以工作。你可以發佈你的其他代碼嗎? (您訪問數組並獲取空白的部分) – cbush06