我正在使用md auto complete for Auto Segessions。当我输入文本时,我想进行服务器调用以获得结果。但是当我必须在控制器脚本或服务脚本中控制它时,我想中止以前的服务器调用。?
<md-autocomplete flex required flex-gt-sm="25
md-selected-item="school.college_name"
md-clear-button="true"
md-input-name="school.college"
md-input-minlength="3"
md-input-maxlength="40"
md-no-cache="noCache"
md-search-text="searchText"
md-items="item in ctrl.academyschoolsList"
md-search-text-change = "ctrl.searchAcademySchools(searchText)"
md-item-text="item.name">
<md-item-template>
<span class="item-title">
<span> {{item.name}}, {{item.city}}, {{item.state}}, {{item.zip}}
</span>
</span>
</md-item-template>
<div ng-messages="ctrl.leadEntryForm.school.college.$error">
<div ng-message="required">This field is required</div>
</div>
</md-autocomplete>
function searchAcademySchools(query) {
ctrl.academyschoolsList = [];
if (query.length > 3) {
C2Services.getAcademySchools(query).then(function (data) {
angular.forEach(data, function (value, key) {
ctrl.academyschoolsList.push(value);
});
});
}
}
答案 0 :(得分:1)
XHR一旦启动就无法中止,但新结果可以取代旧结果:
function searchAcademySchools(query) {
if (query.length > 3) {
C2Services.getAcademySchools(query).then(function (data) {
ctrl.academyschoolsList = data;
});
} else {
ctrl.academyschoolsList = [];
};
}
在此示例中,每个XHR都将替换旧结果。