【问题标题】:Select an option from a dropdown by its value - Angularjs按其值从下拉列表中选择一个选项 - Angularjs
【发布时间】:2016-07-06 14:42:07
【问题描述】:

我有一个从数据库中提取下拉选项的工厂:

app.factory('populateDDL', function($http) {
    'use strict';
    return function (tbl,key) {
        var json,
            data = JSON.stringify({
                sKey: key,
                sTableName: tbl
            });
        return $http.post('util.asmx/populateDDL',data).
        then(function(response) {
            return JSON.parse(response.data.d);
        });
    };
});

然后我将该数据应用到我的控制器中的下拉列表/选择:

app.controller('userSettings', function($scope, $http, populateDDL) {
            $scope.clinicLocations = new populateDDL('locations',key).
            then(function(response) {
                console.log(response);
                $scope.clinicsDDL = response.locations;
                $http({
                    url: 'util.asmx/returnUser', 
                    method: 'POST',
                    data: {sUserId: uid},  
                })
                .success(function(user) {
                    $scope.user = user.d;
                })
                .error(function(data, status, headers, config) {
                    console.log(data+', '+status+', '+headers+', '+config);
                });
            });
        });

选择标签的外观如下:

<select ng-model="clinicsDDL.item" ng-options="item.name for item in clinicsDDL track by item.id" ngRequired />

$scope.clinicsDDL 中的下拉列表数据如下所示:

[{
    "id": "19",
    "name": "other clinic"
}, {
    "id": "39",
    "name": "My clinic"
}]

从数据库返回的用户数据如下所示:

{
    "__type": "User",
    "FirstName": "Erik",
    "LastName": "Grosskurth",
    "DefaultLocationId": "39"
}

在我的控制器中,我填充下拉列表/选择,然后获取用户 ID 并将其提交给数据库以获取用户信息。此用户信息将返回。从那时起,我想根据从数据库返回的“DefaultLocationId”从先前填充的列表中选择一个特定选项。

我该怎么做呢?

$scope.clinicsDDL.item = $scope.clinicsDDL[0];

^^^ 不起作用,因为我不能依赖数组总是形成相同的。我需要基于“DefaultLocationId”而不是整数来定位数组项。

在 jQuery 中,您可以使用 $('#clinicsDDL option[value="19"').doSomething(); 来定位选择器

我在想它会像这样的角度:

$scope.clinicsDDL.item = $scope.clinicsDDL[value="19"];

^^^ 但这也不起作用

【问题讨论】:

    标签: angularjs drop-down-menu html-select angular-ngmodel angularjs-ng-options


    【解决方案1】:

    预选仅使用参考。当您想要选择一个元素并且您不知道哪个字段包含它时,您必须搜索它。而不是使用 array.filter() 你应该使用 lodash 的 find 函数 (https://lodash.com/docs#find)

    //preselect DefaultLocationId 39
    $scope.clinicsDDL.item = _.find($scope.clinicsDDL, {DefaultLocationId : 39});
    

    _.find(...) 将在找不到项目时返回 undefined。因此,您可以将其保留为未定义或使用默认值,例如

    $scope.clinicsDDL.item = _.find($scope.clinicsDDL, {DefaultLocationId : 39}) || $scope.clinicsDDL[0];
    

    【讨论】:

    • angular.js:13424 ReferenceError: _ is not defined
    • 你必须在你的项目和 html 中包含 lodash。见lodash.com
    • 这不是 Angular 解决方案。我也可以在其他插件中完成单行修复。我正在寻找一个纯 Angular 解决方案,谢谢
    • 然后你可以使用 array.filter() 这种方法是纯javascript。缺点是函数调用将遍历数组的所有元素并返回一个新数组。当您不想使用纯 Javascript 并强制使用 AngularJS 解决方案时,您可以使用 filter 过滤器。将 $filter 服务注入您的控制器并调用 $filter("filter")($scope.clinicsDDL, {DefaultLocationId : 39}) 这里同样的缺点是您遍历数组中的所有元素。不要忘记 tis 返回一个数组。当 id 唯一时,您必须访问索引 0 处的返回值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    相关资源
    最近更新 更多