绑定基于下拉列表的值

时间:2019-02-20 15:39:55

标签: html angularjs

我必须根据用户ID在下拉列表中获取国家/地区。我已经尝试过下面的代码,但是它不起作用。有人可以帮我吗?

HTML:

<h1 align="center">Audit</h1>
<p>Country* :</p> 
<select class="form-control flat-control-inner" id=""  ng-model="user.id">
  <option value = "" > --Please Select-- </option>
  <option data-ng-repeat="c in countryList" ng-value="{{c.id}}" ng-selected="user.id==c.id">{{c.country}}</option>
</select>

控制器:

var user={};
    $scope.user.id=1;
    $scope.userId=1;
    $scope.rgId=1;
    $scope.ctryId=1;
    $scope.trId=1;
    $scope.ctyId=1;

    $scope.fetchCountryList = function(userId){

          $(".loader").show();           
          AuditorInfoService.fetchCountryList($scope.userId).then(function(response){
               $scope.countryList = response.data;
              console.log($scope.countryList);

        },function(response){
            $(".loader").fadeOut("slow");
            });        
      };
      $scope.fetchCountryList($scope.userId);

1 个答案:

答案 0 :(得分:0)

您可以通过两种方式完成您想要的事情:

  1. 更改data-ng-repeat中的ng-repeat
  2. 使用ng-options。这是专门为select

    设计的Angular指令

    <select class="form-control flat-control-inner" ng-options="c in countryList" ng-selected="user.id==c.id" ng-model="user.id"></select>

文档here

相关问题