Angular ng-options表现奇怪

时间:2014-10-24 08:15:59

标签: javascript html angularjs angular-strap

我有带角带的这个typahead:

<div class="form-group" ng-class="newGroupForm.placeReference.$invalid ? 'has-error' : ''">
    <label for="placeReference" class="col-lg-2 control-label">Group Location</label>
    <div class="col-lg-10">
        <input type="text" name="placeReference" 
               ng-model="newGroup.reference" 
               ng-options="place.reference as place.name 
                           for place in getPlaces($viewValue)" 
               bs-typeahead min-length="0" required >
    </div>
</div>

getPlaces返回如下所示的对象数组:

{
   reference: "ccj32213SIJD",
   name: "some name",
}

当我输入时,我得到了正确的结果,但是当我选择了wonted选项时,我在输入中看到的值是引用(而不是名称)。

任何人都可以指出我的错误吗?

suggestions reference instead of name

这是控制器代码:

$scope.getPlaces = function(viewValue) {
    var input = viewValue || $scope.currentPlace;
    return googleService.placesAutocomplete(input).then(
        function(places) {
            return places;
        }
    );
};

1 个答案:

答案 0 :(得分:0)

如果此处ng-options的行为与<select ng-options>完全相同(抱歉,我不熟悉bs-typeahead指令),那么您应将其更改为:

<input ... 
    ng-model="selectedPlace" 
    ng-options="place as place.name for place in getPlaces($viewValue)">

然后你可以在其他地方使用获取名称:

<div>{{selectedPlace.name}}</div>

最好是获得实际的place对象{name: "..", reference: "..."},但如果您只需要该名称,那么您可以这样做:

<input ... 
        ng-model="selectedPlaceName" 
        ng-options="place.name as place.name for place in getPlaces($viewValue)">