2016-11-04 55 views
0

我有一個表中填充了從使用AngularJS捕獲的服務中檢索到的對象的記錄。我在表格中有一個複選框,可以對該行進行編輯。在行內,我有一個選擇框,當我點擊行中的「編輯」複選框時,我仍然需要將其默認值設置爲行中的原始值,同時仍然使用所有其他值來填充選擇內容。設置在編輯之前將值選擇爲值

下面是填充表格和選擇框的代碼。

 <thead> 
      <tr> 
       <th class="col-lg-2">Sub Program</th> 
       <th class="col-lg-6">Description</th> 
       <th class="col-lg-2">Program</th> 
       <th class="col-lg-1">Edit</th> 
       <th class="col-lg-1">Update/Delete</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr ng-repeat="timeAllocationSubProgram in timeAllocationSubPrograms"> 
       <td ng-hide="checked">{{timeAllocationSubProgram.subProgramName}}</td> 
       <td ng-hide="checked">{{timeAllocationSubProgram.subProgramDescription}}</td> 
       <td ng-hide="checked">{{timeAllocationSubProgram.programName}}</td> 
       <td ng-show="checked"> 
        <input class="form-control" value="{{timeAllocationSubProgram.subProgramName}}" 
          ng-model="timeAllocationSubProgram.subProgramName" /> 
       </td> 
       <td ng-show="checked"> 
        <input class="form-control" value="{{timeAllocationSubProgram.subProgramDescription}}" 
          ng-model="timeAllocationSubProgram.subProgramDescription" /> 
       </td> 
       <td ng-show="checked"> 
        <select class="form-control" ng-options="timeAllocationProgram as timeAllocationProgram.programName 
          for timeAllocationProgram in timeAllocationPrograms track by timeAllocationProgram.programName" ng-model="timeAllocationProgram" 
          ng-change="selectProgram(timeAllocationProgram)"></select> 
       </td> 
       <td> 
        <input type="checkbox" ng-model="checked" aria-label="Toggle ngShow"/> 
       </td> 
       <td> 
        <button class="btn btn-xs btn-primary" ng-click="editTimeAllocationSubProgram(timeAllocationSubProgram)" 
          ng-show="checked"> 
         Update 
        </button> 
        <button class="btn btn-xs btn-primary" ng-click="deleteTimeAllocationSubProgram(timeAllocationSubProgram)" 
          ng-hide="checked"> 
         Delete 
        </button> 
       </td> 
      </tr> 
     </tbody> 
    </table> 

回答

0

隨着我卑微的經驗,爲默認值設置爲鏈接到NG-模型角度投入的最好辦法,就是設定的默認值了NG-模型。

NG選項這裏remplacing

<options ng-repeat="timeAllocationProgram in timeAllocationPrograms"> 

因此,如果timeAllocation(因爲它寫的是什麼在NG-模型指令)設置爲你想要的時候,你從服務加載數據的默認值,它應該顯示它的權利。

的主題似乎已經被回答爲好,這裏的鏈接,用plunker:

所有的

how to use ng-option to set default value of select element

+0

首先,感謝您回覆。非常感謝。我認爲它很接近,但不是我所需要的(至少我不認爲 - 我對Angular來說是新手)。我打算在單擊複選框之前根據單元格中的內容設置默認值。根據我選擇編輯哪一行,它可能會有所不同。你提供的例子很好地設置基於靜態代碼的默認值,但不是動態的(至少我可以解釋)。 – spice

+0

你可以讓你的問題成爲JsFiddle嗎? https://jsfiddle.net/ 我有點想念你的JavaScript來幫助你。 – Alburkerk