나는 메리 페어헤드 각 ᅴ 사용하여 다음과 같은 방법으로:
<input type="text" ng-model="myModel" typeahead="o.value as o.text for o in options | filter:$viewValue | limitTo:5" typeahead-editable="false" />
비네트 모델로 다음과 같습니다.
var options = [
{"value": 1, "text": "value1"},
{"value": 2, "text": "value2"},
...
];
하지만 내가 항목을 선택할 때 올바른 fs@snapa 옵션뿐 텍스트 안에 텍스트 값을 보여준다. 그 가치를 제대로 경계 디렉토리에만 모델이 아닌 전체 모델을 객체).
텍스트 상자 안에 있는 ", text" 표시하십시오 수 있다. 아닌 value" ";) 그 후 선택물을 바인딩하면 값을 유지하는 모델 (예: 내가 선택할 때 특정 " text"; 이 모델은 업데이트됩니다 value" ";)?
HTML:
<input type="text"
ng-model="myModel"
typeahead="o.value as o.text for o in options | filter:$viewValue | limitTo:5"
typeahead-editable="false"
typeahead-input-formatter="formatLabel($model)"
/>
앙굴라이스 컨트롤러 기능:
$scope.formatLabel = function(model) {
for (var i=0; i< $scope.options.length; i++) {
if (model === $scope.options[i].value) {
return $scope.options[i].text;
}
}
};
[프룬커]: http://plnkr.co/edit/pFsPx0lTM7OZVwF97m1l? p = 미리봅니다
그래서 일부 메리 페어헤드 제안됩니다 뿐만 아니라 같은 일을 시도할 수 있다
<input type="text"
ng-model="myModel"
typeahead="o as o.text for o in options | filter:$viewValue | limitTo:5"
typeahead-editable="false"
typeahead-on-select="model=$item.value"
/>
이를 통해 텍스트 또는 레이블 표시되었습니까 하지만 기본 값은 바뀌었다.
이곳에서 모두가 사용하는 사람을 위한 대표 포맷터 로다시 또는 밑줄:
function formatTypehead(array,id){
var o = _.find(array,{id:id});
return (o?o.displayName || id:id);
}
그리고 html:
<input type="text"
uib-typeahead="s.id as s.displayName for s in companies | filter:$viewValue | limitTo:8"
typeahead-input-formatter="formatTypehead(companies, $model)"
ng-model="model.company"
>
뭐, 지금까지 통해 해결책을 찾았다고 디렉티브에는.
HTML
<div my-autocomplete my-autocomplete-source="element" my-autocomplete-model="obj[element.model]"></div>
디렉티브을
app.directive('myAutocomplete', function() {
return {
restrict: 'A',
replace: true,
template: '<input type="text" name="{{myAutocompleteSource.model}}" placeholder="{{myAutocompleteSource.label}}" ng-model="selected" typeahead="o as o.text for o in myAutocompleteSource.options | filter:$viewValue | limitTo:5" typeahead-editable="false" />',
scope: {
myAutocompleteSource: '=',
myAutocompleteModel: '='
},
controller: function($scope) {
$scope.selected = null;
$scope.$watch('selected', function() {
$scope.myAutocompleteModel = ($scope.selected && 'value' in $scope.selected) ? $scope.selected.value : null;
});
}
};
});
뭐. 물론 이것은 단지 속임수. 다운로드되었는지 있나 싶다 클리너, 좀 더 자연스러운 길일. 수정 없이 사용하여 코드 또는 디렉티브을.
가져다줄래요 이:
uib-typeahead="o as o.RagioneSociale for o in main.getLocation($viewValue)"
대신:
typeahead="o as o.RagioneSociale for o in main.getLocation($viewValue)"
정말 유용
내가 이렇게 json 브라운아저씨의 했다.
[{"RagioneSociale":"Politi Real Estate sas","IDAnagrafica":"2516"},{"RagioneSociale":"COND METROPOLITAN","IDAnagrafica":"4325"}]
Model: {{asyncSelected | json}}
<input type="text" ng-model="asyncSelected" uib-typeahead="o as o.RagioneSociale for o in main.getLocation($viewValue)" typeahead-loading="loadingLocations" typeahead-no-results="noResults" class="form-control">
또한 이 같은 값을 갖는 드롭다운이 메뉴 부트해 레지오니소샤리 됐지 뭘 어디서 텍스트와 id 및 인쇄 및 모델 모두 볼 수 있는 그들 라이너스셀레스테드 일반 {{}}