为什么在AngularJS中获得如此[1]的价值

时间:2018-02-11 20:40:32

标签: angularjs asp.net-core-mvc

我需要从表中获取id,但我得到的是这样的[1]。我只需要1

控制器

 public JsonResult GetLineManagerId(string LineManagerId)
        {
var lineManagerId = db.Employees
       .Where(x => x.FirstName == LineManagerId)
       .Select(x =>x.Id);
            return Json(lineManagerId);
        }

$scope.GetLineManagerId = function (LineManagerId) {
            $http.get('/Official/GetLineManagerId?LineManagerId=' + LineManagerId).then(function (data) {
                console.log(data);
                $scope.lineManagerId = data.data;
                $scope.empModel.lineManagerId = $scope.lineManagerId[0].lineManagerId;

            });
        };

HTML

 <select ng-model="empModel.name" id="" name="Name" class="form-control" 
                                    ng-change="GetLineManagerId(empModel.name)"
                                    ng-click="GetLineManagerId(empModel.name)"
                                    ng-options="d.name as d.name for d in name">
                                <option></option>


     </select> 
  <select ng-model="empModel.id" id="" name="Id" class="form-control"
                                  >
                                <option>{{lineManagerId}}</option>
                            </select>  

当我点击第一个下拉列表时,我会得到[1]这样的ID ...我只需要这样的号码1

我需要在下拉列表中获取ID号,如果有人帮我在文本输入中获取id而不是使用下拉列表。

2 个答案:

答案 0 :(得分:2)

试试这个,请不要使用ng-点击这里

 <select ng-model="empModel.name" id="" name="Name" class="form-control" 
                                    ng-change="GetLineManagerId(empModel.name)"
                                    ng-options="d.name as d.name for d in name">
                                <option></option>

使用此

加载linemanager
  <select ng-model="empModel.id" id="" name="Id" class="form-control"
                                ng-options="d.lineManagerId as d.lineManagerId for d in lineManagerId"
                                  >
                             <option></option>   
                            </select>  

我认为你的其他部分将保持不变

答案 1 :(得分:0)

您正在返回数组/枚举而不是单个值。

 <script src="data.js"></script>
 <script type="text/javascript">
     function buildSelect() {
        // ...
     }
 </script>

您必须选择一个结果,如

public JsonResult GetLineManagerId(string LineManagerId)
{
    var lineManagerId = db.Employees
       .Where(x => x.FirstName == LineManagerId)
       .Select(x =>x.Id); // returns an IEnumerable<T> for whatever Id is

    return Json(lineManagerId);
}