如何自动选择值angularjs

时间:2016-11-21 17:47:34

标签: javascript angularjs

下拉是空白的我想要自动选择值理查德怎么做,我是角色的新人

  <select class="form-control" name="customer" id="customer" ng-model="postjobcustomerCtrl.selectedCustomer" ng-options="customer.full_name for customer in postjobcustomerCtrl.atlasCustomers  | filter:{full_name: Profile.full_name} track by customer.user_id"
ng-change="postjobcustomerCtrl.selectDefaultAddress()" required></select>

enter image description here

5 个答案:

答案 0 :(得分:0)

您希望使用Angular的ng-Init内联设置所选值。

<select class="form-control" name="customer" id="customer" ng-model="postjobcustomerCtrl.selectedCustomer" ng-options="customer.full_name for customer in postjobcustomerCtrl.atlasCustomers  | filter:{full_name: Profile.full_name} track by customer.user_id"
ng-change="postjobcustomerCtrl.selectDefaultAddress()" ng-init="postjobcustomerCtrl.selectedCustomer = 'Richard'" required></select>

答案 1 :(得分:0)

postjobcustomerCtrl中,使用 Richard ng-model初始化选择postjobcustomerCtrl.selectedCustomerid)。

// angularjs controller

atlasCustomers = [{full_name : "Richard", user_id: 1}]        
postjobcustomerCtrl.selectedCustomer = atlasCustomers[0].atlasCustomers.user_id;

答案 2 :(得分:0)

如果您有以下内容......

<select ng-model="item">
  <option value="1">Bob</option>
  <option value="2">James</option>
  <option value="3">Karl</option>
</select>

...你希望詹姆斯默认被选中,然后在角度控制器中,你只需这样做:

$scope.item = 2;

像这样:

&#13;
&#13;
angular.module('app', [])
.controller('ctrl', function($scope){
  $scope.item = 2;
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <select ng-model="item">
    <option value="1">Bob</option>
    <option value="2">James</option>
    <option value="3">Karl</option>
  </select>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您需要做的就是在此处使用适当的值(“Richard”)初始化模型以获取默认值

var app = angular.module('app', []);

app.controller('MainCtrl', ['$scope', function ($scope, $http, uiGridConstants) {
  $scope.postjobcustomerCtrl ={};
  $scope.postjobcustomerCtrl.atlasCustomers =[{"full_name":"Richard"},{"full_name":"Colins"},{"full_name":"Steve"}];
  
  $scope.postjobcustomerCtrl.selectedCustomer =$scope.postjobcustomerCtrl.atlasCustomers[0]
  $scope.postjobcustomerCtrl.selectDefaultAddress =function(){
    
  }
  
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<div ng-app ="app" ng-controller="MainCtrl">
  
   <select class="form-control" name="customer" id="customer" ng-model="postjobcustomerCtrl.selectedCustomer" ng-options="customer.full_name for customer in postjobcustomerCtrl.atlasCustomers"
ng-change="postjobcustomerCtrl.selectDefaultAddress()" required></select>
 
</div>


 

答案 4 :(得分:0)

 myApp.Controller("MyCltr, function($scope){
     // first fetch data from db
     // bind data to drop-down
     // then set selected value.
     $scope.postjobcustomerCtrl.selectedCustomer = "Richard";

});

请确保在设置此值后,如果您的下拉列表与数据绑定。