angularjs自动选择下拉

时间:2017-03-15 08:35:24

标签: angularjs angularjs-select

在以下功能中我得到了响应和长度 perfiosAnalysisData

function getPerfiosData() {

      var tribeId = vm.currentTribeId;

      getPerfiosAnalysisData(tribeId).then(function(response){
        vm.perfiosAnalysisData = response.data;
        /* Check Select Option Length */            
            $scope.lengthData = vm.perfiosAnalysisData.institutions.length;     
        /* Check Select Option Length */        
        vm.isPerfiosEnabled = response.is_enabled;
        setChartDataConfig();
        getDrawGraph();
      },function(err){
        if(err.status === 412) {
          vm.perfiosNotPermitted = true;
        }
      });
    }

所以如果长度为1,我想自动选择选项并调用ng-change函数。

<select class="perf-select" ng-model="viewProfileCtrl.monthsForm.institution"
                           ng-options="inst.institution.institution_id as inst.institution.name for inst in viewProfileCtrl.perfiosAnalysisData.institutions"
                           ng-change="viewProfileCtrl.setCurrMonthInsti(viewProfileCtrl.monthsForm.institution)">
                           <option value=""  selected>Select a Bank
                        </select>

如何实现这个目标?

1 个答案:

答案 0 :(得分:0)

如果lengthData等于1,则将第一个对象分配给ng模型变量并调用ng更改函数,如下所示

function getPerfiosData() {
    var tribeId = vm.currentTribeId;
    getPerfiosAnalysisData(tribeId).then(function(response) {
        vm.perfiosAnalysisData = response.data;
        /* Check Select Option Length */
        $scope.lengthData = vm.perfiosAnalysisData.institutions.length;


        if ($scope.lengthData === 1) {
            $scope.viewProfileCtrl.monthsForm.institution = $scope.viewProfileCtrl.perfiosAnalysisData.institutions[0];
            $scope.viewProfileCtrl.setCurrMonthInsti($scope.viewProfileCtrl.monthsForm.institution);
        }


        /* Check Select Option Length */
        vm.isPerfiosEnabled = response.is_enabled;
        setChartDataConfig();
        getDrawGraph();
    }, function(err) {
        if (err.status === 412) {
            vm.perfiosNotPermitted = true;
        }
    });
}