![](/img/trans.png)
[英]md-autocomplete with error: Cannot read property 'success' of undefined
[英]md-autocomplete returns TypeError: Cannot read property 'then' of undefined
使用angular material
的md-autocomplete
組件我遇到了一個問題:
<md-autocomplete
required
md-search-text="searchTxt"
md-selected-item-change="setModelValue(item.name)"
ng-model="searchTxt"
md-search-text-change = "searchItem(searchTxt)"
md-items="item in pickerResult"
md-item-text="item.name"
md-min-length="0"
md-delay="100"
placeholder="Search...">
<md-item-template>
<span md-highlight-text="searchTxt" md-highlight-flags="^i">{{item.title}}</span>
</md-item-template>
<md-not-found>
No results <span data-ng-if="form.detailModel.aspectName != null">per</span> {{form.detailModel.aspectName}}
</md-not-found>
</md-autocomplete>
這是我的控制器中的功能
$scope.searchAspect = function(searchStr) {
if(!searchStr) {
var searchStrEncoded = "";
} else {
var searchStrEncoded = escape(searchStr);
}
var url = "/api/url&searchTxt=" + searchStrEncoded;
$http({
url: url,
method: 'GET'
}).success(function (data, status, headers, config) {
$scope.pickerResult = data.data;
});
};
如果我輸入一些東西,我就會得到數據。 但是在輸入中模糊時,我收到此錯誤: TypeError: Cannot read property 'then' of undefined
我無法取回我的數據。 我試圖以這種方式更改 md-items 指令
md-items="item in searchItem(searchTxt)"
我沒有收到錯誤,但即使 http 調用成功,自動完成也沒有顯示任何結果。 有什么想法嗎?
編輯承諾
$scope.searchAspect = function(searchStr) {
if(!searchStr) {
var searchStrEncoded = "";
} else {
var searchStrEncoded = escape(searchStr);
}
var deferred = $q.defer();
var url = "/api/url&searchTxt=" + searchStrEncoded;
$http({
url: url,
method: 'GET'
}).success(function (data, status, headers, config) {
deferred.resolve(data.data);
$scope.pickerResult = data.data;
}).error(deferred.reject);
return deferred.promise;
};
同樣的錯誤
試試這個
<md-autocomplete
required
md-search-text="searchTxt.val"
md-items="item in searchAspect(searchTxt)"
md-item-text="item.name"
md-min-length="0"
md-delay="100"
placeholder="Search...">
<md-item-template>
<span md-highlight-text="searchTxt" md-highlight-flags="^i">{{item.title}}</span>
</md-item-template>
<md-not-found>
No results <span data-ng-if="form.detailModel.aspectName != null">per</span> {{form.detailModel.aspectName}}
</md-not-found>
</md-autocomplete>
$scope.searchAspect = function(searchStr) {
if(!searchStr.val) {
var searchStrEncoded = "";
} else {
var searchStrEncoded = escape(searchStr);
}
var url = "/api/url&searchTxt=" + searchStrEncoded;
return $http({
url: url,
method: 'GET'
}).then(function (data) {
return data.data;
});
};
據我了解,md-items 屬性需要一個承諾,而您提供的是一個數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.