Angularjs使用和不使用http自动完成json [英] Angularjs autocomplete json with and without http
问题描述
在我的代码中我使用angular(material)自动完成,我需要从外部php文件中获取数据。
HTML:
In my code i use angular (material) autocomplete and i need to get data from external php file.
HTML:
<div ng-controller="search_interest" layout="column">
<md-content class="md-padding" layout="column">
<md-autocomplete
md-selected-item="selectedItem"
md-search-text="searchText"
md-items="item in getInterest1(searchText)"
md-item-text="item.name"
placeholder="Search for a vegetable">
<span md-highlight-text="searchText">{{item.name}} :: {{item.type}}</span>
</md-autocomplete>
</div>
当我返回手工制作JSON时,自动完成效果很好:
When i return an "handmade" JSON, autocomplete works well:
//RETURN: results [{"name":"Broccoli","type":"Brassica"}]
$scope.getInterest1 = function() {
var results = [
{
'name': 'Broccoli',
'type': 'Brassica'
}];
console.log('results', JSON.stringify(results));
return results;
};
但是当我使用$ http.get调用相同的JSON时,它不会显示自动完成的结果,也没有任何事情发生。
But when i call the same JSON using $http.get it doesn't show results in autocomplete and nothing happends.
//RETURN: results [{"name":"Broccoli","type":"Brassica"}]
$scope.getInterest2 = function() {
$http.get("../inc/users/search_interest.php?query=" + $scope.searchText)
.success(function(results) {
//$scope.interest = results;
console.log('results', JSON.stringify(results));
return results.data;
});
};
但我可以在控制台中看到结果是等于。
在我的php文件(由$ http.get调用)我用json-encode返回JSON:
But i can see in console that results are equals.
In my php file (which is called by $http.get) i return JSON with json-encode:
$row_set = array('name' => 'Broccoli', 'type'=> 'Brassica');
echo json_encode(array($row_set));
也许php json_encode返回坏形成json?
谢谢
Maybe php json_encode return a bad formed json?
Thanks
推荐答案
scope.getInterest1 = 函数(){
var results = [
{
' name':' Broccoli',
' type':' Brassica'
}];
console .log(' results', JSON .stringify(results));
返回结果;
};
scope.getInterest1 = function() { var results = [ { 'name': 'Broccoli', 'type': 'Brassica' }]; console.log('results', JSON.stringify(results)); return results; };
但是当我使用
But when i call the same JSON using
http.get调用相同的JSON时,它不会显示结果自动完成,没有任何动静。
http.get it doesn't show results in autocomplete and nothing happends.
//RETURN: results [{"name":"Broccoli","type":"Brassica"}]
scope.getInterest2 = function (){
scope.getInterest2 = function() {
这篇关于Angularjs使用和不使用http自动完成json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!