2016-11-30 42 views
0

我有以下HTML代碼,並且希望加載選擇下拉菜單以自動選擇來自DB的條目 - 在本例中是來自rule.id變量。我如何在AngularJS中做到這一點?在AngularJS中將下拉列表設置爲特定值

<div class="card-second-input" ng-if="rule.unfolded"> 
    <select ng-init="rule.actionId" class="rectangle-30" ng-class="rules-option" ng-model="selectedAction" ng-options="team.name for team in teams track by team.id"> 
    <option class="rules-option" value="">Select Team</option> 
    </select> 
</div> 

P.S我採用了棱角分明的1.x

+0

您希望顯示爲默認值,在從數據庫加載數據後,在模型'selectedAction'中爲其賦值。 –

回答

2

,基本建立了NG-模型值所需的團隊目標可以使這種變化。

這應該在控制器內完成。

請看看到下面的代碼: -

 <!DOCTYPE html> 
      <html> 
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"> 
      </script> 
      <body> 

      <div ng-app="myApp" ng-controller="myCtrl"> 
       <select class="rectangle-30" ng-model="selectedAction" ng-options="team.name for team in teams track by team.id"> 
       <option value="">Select Team</option> 
       </select> 
      <script> 
       var app = angular.module('myApp', []); 
       app.controller('myCtrl', function($scope) { 

       $scope.teams = [ 
        {"name": "A", "id" : 1}, 
        {"name": "B", "id" : 2}, 
        {"name": "C", "id" : 3}, 
        {"name": "D", "id" : 4} 
       ]; 
       $scope.selectedAction = {"name": "B", "id" : 2}; 
       }); 

      </script> 

      </body> 
     </html> 

聚焦線 $ scope.selectedAction = { 「名稱」: 「B」, 「ID」:2};

+0

@Zabs ...請讓我知道任何查詢 –

+0

謝謝,這是非常清楚和簡潔非常讚賞:) – Zabs

1

使用ng-option這樣

ng-options="team.id as team.name for team in teams track by team.id" 

和設置您的項目ID來模擬這樣$scope.selectedAction = ieamId從控制器,它從DB獲得解決方案。

相關問題