gpt4 book ai didi

angularjs - $http 问题 - 在 md-autocomplete Angular Material 中解决 promise 之前无法返回值

转载 作者:行者123 更新时间:2023-12-05 00:19:32 25 4
gpt4 key购买 nike

我正在使用 Angular Material md-autocomplete在我的项目中。我正在使用 $http 通过 ajax 调用从服务主机获取建议列表。服务。

Issue: $http issue - Values can't be returned before a promise is resolved in md-autocomplete Angular Material

My Requirement: I need an updated Suggestion List using remote data sources in md-autocomplete Angular Material - Ajax $http service.



我使用了 Angular Material 链接 https://material.angularjs.org/latest/demo/autocomplete 中提到的方法

源代码:

场景一:

HTML 源代码:
<md-autocomplete flex required
md-input-name="autocompleteField"
md-no-cache="true"
md-input-minlength="3"
md-input-maxlength="18"
md-selected-item="SelectedItem"
md-search-text="searchText"
md-items="item in querySearch(searchText)"
md-item-text="item.country" Placeholder="Enter ID" style="height:38px !important;">
<md-item-template>
<span class="item-title">
<span md-highlight-text="searchText" md-highlight-flags="^i"> {{item.country}} </span>
</md-item-template>
</md-autocomplete>

AngularJS 脚本:
//bind the autocomplete list when text change
function querySearch(query) {
var results = [];
$scope.searchText = $scope.searchText.trim();
if (query.length >=3) {
results = LoadAutocomplete(query);
}
return results;
}

//load the list from the service call
function LoadAutocomplete(id) {
var countryList = [];
$http({
method: "post",
url: "https://www.bbminfo.com/sample.php",
params: {
token: id
}
})
.success(function (response) {
countryList = response.records;
});

return countryList;
}

场景二:

带有 AngularJS 源代码的 HTML:

<!DOCTYPE html>
<html>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-aria.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-messages.min.js"></script>

<!-- Angular Material Library -->
<script src="http://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<p>Person to Select:</p>

<md-autocomplete
ng-disabled="isDisabled"
md-no-cache="noCache"
md-selected-item="selectedItem"
md-search-text-change="searchTextChange()"
md-search-text="searchText"
md-selected-item-change="selectedItemChange(item)"
md-items="item in Person"
md-item-text="item.Name"
md-min-length="0"
placeholder="Which is your favorite Person?">
<md-item-template>
<span md-highlight-text="ctrl.searchText" md-highlight-flags="^i">{{item.country}}</span>
</md-item-template>
<md-not-found>
No Person matching "{{searchText}}" were found.
</md-not-found>
</md-autocomplete>
<br/>
</div>

<script>
var app = angular.module('myApp', ['ngMaterial']);

app.controller('myCtrl', function ($scope, $http, $q) {

$scope.searchText = "";
$scope.Person = [];
$scope.selectedItem = [];
$scope.isDisabled = false;
$scope.noCache = false;

$scope.selectedItemChange = function (item) {
alert("Item Changed");
}
$scope.searchTextChange = function () {

$http({
method: "POST",
url: "https://www.bbminfo.com/sample.php",
params: {
token: $scope.searchText
}
})
.success(function (response) {
$scope.Person = response.records;
});
}

});
</script>
</body>
</html>


场景一 ,我使用函数获取过滤列表 md-items="item in querySearch(searchText)" .但在 场景二 , 我用了 $scope变量 md-items="item in Person"
请引用快照

快照 1:

Autocomplete Listing Issue - UI

我在这里搜索 印度人 但它显示了 的结果印度 .我在 中调试了这个问题火狐浏览器 Firebug ,见上文 快照 1 它显示,请求是针对搜索词 发送的印度人 通过 POST 方法,我成功得到了一个匹配项作为 JSON 对象的响应,显示在 的底部快照 1

The issue I find out in this case, the Values can't be returned before a promise is resolved



我试过的步骤:

案例一 : 我使用了 AngularJS filter在 UI md-items="item in Person | filter: searchText" ,它给出了先前获取的远程数据的过滤列表,而不是当前获取的远程数据。在退格文本框中的字符时,它会显示不正确的建议列表。

案例二 : 我尝试通过调用 $scope.$apply() 来更新 UI 中的更改在 $http 内服务,但它失败了。因为 $http服务调用 $scope.$apply()默认情况下,显示它会引发错误 错误:[$rootScope:inprog]... .最后在这次尝试中我失败了。

案例3 :我创建了一个函数,在函数中我手动调用了 $scope.$apply() ,在函数中,我手动将一个虚拟项目推送并弹出到 $scope md-autocomplete 中绑定(bind)的变量.但我在这次尝试中失败了。因为在这里我也得到了与快照中相同的输出。
function Ctrlm($scope) {
$scope.messageToUser = "You are done!";
setTimeout(function () {
$scope.$apply(function () {

$scope.dummyCntry = [
{
sno: 0,
country: ""
},
];

$scope.Person.push($scope.dummyCntry);

var index = $scope.Person.indexOf($scope.dummyCntry);
$scope.Person.splice(index, 1);

});
}, 10);
}

案例4 :我在 $scope.$watchCollection 中采用了与“案例 3”相同的方法。在这里我也遇到了挫折。
$scope.$watchCollection('Person', function (newData, oldDaata) {
$scope.dummyCntry = [
{
sno: 0,
country: ""
},
];

newData.push($scope.dummyCntry);

var index = newData.indexOf($scope.dummyCntry);
newData.splice(index, 1);
});

案例5 : 而不是 $http服务,我使用了 jquery ajax 调用 .我使用了 $scope.apply()手动更新 UI。我再次尝试失败,在这里我也得到了相同的输出。
$scope.searchTextChange = function () {
if (($scope.searchText != undefined) && ($scope.searchText != null)) {

$.ajax({
type: 'GET',
url: "https://www.bbminfo.com/sample.php?token=" + $scope.searchText,
success: function (response) {
$scope.$apply(function () {
$scope.Person = response.records;
});
},
error: function (data) {
$scope.$apply(function () {
$scope.Person = [];
});
},
async: true
});


} else {
$scope.Person = [];
}
}

In all the attempts I can't able to fix the issue.



@georgeawg https://stackoverflow.com/users/5535245/georgeawg建议我发布一个新问题,他说,“写一个新问题,描述你实际想要完成的事情,包括期望的行为,你迄今为止为解决问题所做的工作的总结,以及描述你在解决它时遇到的困难。”

我之前发布的引用问题

发表 1 : http://www.stackoverflow.com/questions/35624977/md-items-is-not-updating-the-suggesion-list-properly-in-md-autocomplete-angular

发布 2 : http://www.stackoverflow.com/questions/35646077/manually-call-scope-apply-raise-error-on-ajax-call-error-rootscopeinprog

My Requirement: I need an updated Suggestion List using remote data sources in Angular Material md-autocomplete - Ajax $http service.



请在这方面帮助我。

For Testing Purpose Use the following Source Code



对远程数据源使用以下 URL: https://bbminfo.com/sample.php?token=ind

远程数据源 URL 包含国家名称列表。

Directly Test the Code by click the below Run Code Snippet button.



使用 AngularJS 源代码完成 HTML:

<!DOCTYPE html>
<html>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-aria.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-messages.min.js"></script>

<!-- Angular Material Library -->
<script src="http://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<p>Country to Select:</p>
<md-content>
<md-autocomplete
ng-disabled="isDisabled"
md-no-cache="noCache"
md-selected-item="selectedItem"
md-search-text-change="searchTextChange()"
md-search-text="searchText"
md-selected-item-change="selectedItemChange(item)"
md-items="item in Person"
md-item-text="item.country"
md-min-length="0"
placeholder="Which is your favorite Country?">
<md-item-template>
<span md-highlight-text="searchText" md-highlight-flags="^i">{{item.country}}</span>
</md-item-template>
<md-not-found>
No Person matching "{{searchText}}" were found.
</md-not-found>
</md-autocomplete>
</md-content>
<br/>
</div>

<script>
var app = angular.module('myApp', ['ngMaterial']);

app.controller('myCtrl', function ($scope, $http, $q) {

$scope.searchText = "";
$scope.Person = [];
$scope.selectedItem = [];
$scope.isDisabled = false;
$scope.noCache = false;

$scope.selectedItemChange = function (item) {
alert("Item Changed");
}
$scope.searchTextChange = function () {

$http({
method: "post",
url: "https://www.bbminfo.com/sample.php",
params: {
token: $scope.searchText
}
})
.success(function (response) {
$scope.Person = response.records;
});
}

});
</script>
</body>
</html>

最佳答案

@KevinB https://stackoverflow.com/users/400654/kevin-b - 给出如何实现的想法。我真的很感谢他...再次感谢凯文...

我得到了确切的解决方案,我需要什么。

源代码是

<!DOCTYPE html>
<html>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-aria.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-messages.min.js"></script>

<!-- Angular Material Library -->
<script src="http://ajax.googleapis.com/ajax/libs/angular_material/1.0.4/angular-material.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<p>Country to Select:</p>
<md-content>
<md-autocomplete
ng-disabled="isDisabled"
md-no-cache="noCache"
md-selected-item="selectedItem"
md-search-text="searchText"
md-items="item in searchTextChange(searchText)"
md-item-text="item.country"
md-min-length="0"
placeholder="Which is your favorite Country?">
<md-item-template>
<span md-highlight-text="searchText" md-highlight-flags="^i">{{item.country}}</span>
</md-item-template>
<md-not-found>
No Person matching "{{searchText}}" were found.
</md-not-found>
</md-autocomplete>
</md-content>
<br/>
</div>

<script>
var app = angular.module('myApp', ['ngMaterial']);

app.controller('myCtrl', function ($scope, $http, $q, GetCountryService) {

$scope.searchText = "";
$scope.Person = [];
$scope.selectedItem = [];
$scope.isDisabled = false;
$scope.noCache = false;

$scope.selectedItemChange = function (item) {
//alert("Item Changed");
}
$scope.searchTextChange = function (str) {
return GetCountryService.getCountry(str);
}

});

app.factory('GetCountryService', function ($http, $q) {
return {
getCountry: function(str) {
// the $http API is based on the deferred/promise APIs exposed by the $q service
// so it returns a promise for us by default
var url = "https://www.bbminfo.com/sample.php?token="+str;
return $http.get(url)
.then(function(response) {
if (typeof response.data.records === 'object') {
return response.data.records;
} else {
// invalid response
return $q.reject(response.data.records);
}

}, function(response) {
// something went wrong
return $q.reject(response.data.records);
});
}
};
});
</script>
</body>
</html>


我在下面的博客中简要解释了 md-autocomplete - http://www.increvcorp.com/usage-of-md-autocomplete-in-angular-material/

关于angularjs - $http 问题 - 在 md-autocomplete Angular Material 中解决 promise 之前无法返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35652828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com