ng-change在选择的选择上发送不同的值

时间:2019-06-25 09:59:08

标签: html angularjs

亲爱的,我在select标签中使用了选择的select下拉列表,并添加了ng-change标签将其绑定到函数。我也做了一个console.log来监视发送的密钥。我发现,当页面加载并从列表中选择一项时,它可以很好地工作并发送正确的密钥。但是,当我选择相同的项目时,它将发送列表中下一个项目的密钥。任何帮助如何解决

HTML

<div class="form-group">

    <select ng-change="GetUsersList(j)" data-placeholder="Choose a Job" 
            ng-model="j" id="chosenValueID" class="chosen-select" 
            tabindex="1"
            ng-options="j.JobKey as j.JobDesc for j in jobs">
    </select>

    <!--<select ng-change="GetUsersList(job)" data-placeholder="Choose a Job" ng-model="job" id="chosenValueID" class="chosen-select" tabindex="2">
        <option  ng-repeat="j in jobs track by $index" value="{{j.JobKey}}">{{j.JobDesc}}</option>
    </select>-->

</div>

Angularjs

 $scope.GetUsersList = function (job) {
        //var jobkey1 = $("#chosenValueID").chosen().val();
        //$scope.jobkey2 = jobkey1.substring(7);
        console.log(job);
        $scope.ShowLoading('slow');
        FieldsSecuritySrv.GetUsersList("FieldsSecurity/GetUsersList?hospid=" + $scope.hospitalid + '&jobkey=' + job).then(function (response) {
            $scope.UsersList = (response.data);

            GetFieldsList();
            GetCategriesList();
            GetPatientSheets();

            $scope.HideLoading('slow');
        })
    }

预先感谢

2 个答案:

答案 0 :(得分:0)

您的HTML中的j变量有点混乱,它为ng-repeat索引并也用作ng-modal,因此也创建了$ scope.j:(

<select ng-change="GetUsersList()" data-placeholder="Choose a Job" ng-model="selectedJob" id="chosenValueID" class="chosen-select"
        tabindex="1" ng-options="j.JobDesc for j in jobs"></select>

成角度:

//initialize the way u like
$scope.selectedJob = $scope.Jobs[0];

$scope.GetUsersList = function () {
        //var jobkey1 = $("#chosenValueID").chosen().val();
        //$scope.jobkey2 = jobkey1.substring(7);
        console.log($scope.selectedJob.jobKey);
        $scope.ShowLoading('slow');
        FieldsSecuritySrv.GetUsersList("FieldsSecurity/GetUsersList?hospid=" + $scope.hospitalid + '&jobkey=' + $scope.selectedJob.jobKey).then(function (response) {
            $scope.UsersList = (response.data);

            GetFieldsList();
            GetCategriesList();
            GetPatientSheets();

            $scope.HideLoading('slow');
        })
    }

答案 1 :(得分:0)

不知道如何绑定作业,但可以尝试...

 angular.module('myApp', [])
    .controller('myCtrl', ['$scope', function($scope) {
    
      $scope.count = 0;
      $scope.jobs=[
                    {JobKey : "1", JobDesc : "IT"},
                    {JobKey : "2", JobDesc : "Sales"},
                    {JobKey : "3", JobDesc : "Marketing"}
                  ];
           
      $scope.GetUsersList = function(job){
      		alert(job);
      }
    }]);
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="myCtrl">
 	<div class="form-group">
    
     	<select ng-change="GetUsersList(j)" data-placeholder="Choose a Job" ng-model="j" 	id="chosenValueID" class="chosen-select" tabindex="1" ng-options="j.JobKey as j.JobDesc for j in jobs">	</select>
        
	</div>
</div>

</body>
</html>