如何通过所选ID顶部显示0位下拉列表

时间:2018-09-12 20:29:08

标签: angularjs angularjs-scope

当我通过ID时如何通过ID取得城市名显示空白我想显示记录名称

$scope.CityList = [{"CityId":1,"CityName":"London"},{"CityId":2,"CityName":"Manchester"},{"CityId":3,"CityName":"Johnes"}]

    <tr ng-repeat="item in ArrTransportDetail">
    <td> <select class="form-control input-height" name="select" ng-model="item.FromCityId"
    ng-options="city as city.CityName for city in CityList ">
     <option value="">Select...</option>
      </select>
      </td>
    </tr>

1 个答案:

答案 0 :(得分:0)

请参见以下示例,并使用city[0].CityName获取CityName:

<!DOCTYPE html>
<html>
<head>
    <script src="scripts/angular-1.7.2/angular.js"></script>
    <script src="scripts/scripts/index.js"></script>
</head>
<body ng-app="myApp" ng-controller="myController" ng-init="init()">
    <form>
        <input type="text" ng-model="cityId" placeholder="Enter your City Id" ng-keyup="cityName = searchCity(cityId)">
    </form>
    <div>{{cityName}}</div>
</body>
</html>

而控制器是:

"use strict";

var myApp = angular.module("myApp", []);

myApp.controller("myController", function ($scope) {

    $scope.CityList = [{"CityId":1,"CityName":"London"},{"CityId":2,"CityName":"Manchester"},{"CityId":3,"CityName":"Johnes"}];
    $scope.cityId = "";
    $scope.cityName = "";

    $scope.searchCity = function (cityId) {
        const city = $scope.CityList.filter((city) => city.CityId === parseInt(cityId));
        return (city !== 'undefined') ? city[0].CityName : "";
    };

});

希望它会对您有所帮助。