我完成了实现我的预期,但有些东西不能按我的意愿工作。 我的意思是每次用新字母填充输入时,都会再次读取所有JSON,我期望通过http请求加载JSON并直接在预加载的数据上应用过滤器。
我的代码如下。
控制器
(function () {
'use strict';
angular.module('MyApp').controller('DemoCtrl', DemoCtrl);
function DemoCtrl($timeout, $q, $log, $http) {
var self = this;
self.simulateQuery = false;
self.isDisabled = false;
self.querySearch = querySearch;
self.selectedItemChange = selectedItemChange;
self.searchTextChange = searchTextChange;
function querySearch(query) {
return $http.get("data.json").then(function (result) {
var results = query ? result.data.filter(createFilterFor(query)) : result.data,
deferred;
return results;
})
}
function searchTextChange(text) {
$log.info('Text changed to ' + text);
}
function selectedItemChange(item) {
$log.info('Item changed to ' + JSON.stringify(item.NAME));
}
function createFilterFor(query) {
// var lowercaseQuery = angular.lowercase(query);
var lowercaseQuery = query;
return function filterFn(item) {
return (item.NAME.indexOf(lowercaseQuery) === 0);
};
}
}
})();
HTML
<html>
<head>
<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js'>
</script>
<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-animate.min.js'>
</script>
<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-route.min.js'></script>
<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-aria.min.js'></script>
<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-messages.min.js'></script>
<script src='https://gitcdn.xyz/repo/angular/bower-material/v0.11.0/angular-material.js'></script>
<script src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-114/assets-cache.js'></script>
<script src='script.js'></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/0.10.0/angular-material.min.css"> </head>
<link rel="stylesheet" href="style.css"> </head>
<body>
<div ng-controller="DemoCtrl as ctrl" layout="column" ng-cloak="" class="autocompletedemoCustomTemplate" ng-app="MyApp">
<md-content layout-padding="" layout="column">
<form ng-submit="$event.preventDefault()">
<md-autocomplete ng-disabled="ctrl.isDisabled" md-no-cache="ctrl.noCache" md-selected-item="ctrl.selectedItem" md-search-text-change="ctrl.searchTextChange(ctrl.searchText)" md-search-text="ctrl.searchText" md-selected-item-change="ctrl.selectedItemChange(item)" md-items="item in ctrl.querySearch(ctrl.searchText)" md-item-text="item.NAME" md-min-length="0" placeholder="Pick an NAME" md-menu-class="autocomplete-custom-template">
<md-item-template> <span class="item-title">
<span> {{item.NAME}} </span> </span> <span class="item-metadata">
<span class="item-metastat">
<strong>{{item.Environnement}}</strong>
</span> <span class="item-metastat">
<strong>{{item.Location}}</strong>
</span> </span>
</md-item-template>
</md-autocomplete>
</form>
</md-content>
</div>
</body>
</html>
data.json
[
{
"NAME":"name1",
"Environnement":"Environnement1",
"Location":"Location1"
},
{
"NAME":"name2",
"Environnement":"Environnement2",
"Location":"Location2"
}
]
另一件事是我的css没有应用。
plnkr:http://plnkr.co/edit/euKrNhMmWGzIx5TnR24L?p=preview
提前致谢。
答案 0 :(得分:1)
您的loadAll()
方法返回一个promise,而不是一个数组;这就是你没有“过滤”功能的原因。你必须在承诺内部进行过滤。也许是这样的:
function querySearch(query) {
return loadAll()
.then(function (repos) {
self.repos = repos.filter(createFilterFor(query));
});
}
答案 1 :(得分:-1)
我建议使用以下代码:
JS :(更新)
function DemoCtrl($timeout, $q, $log, $http) {
var array=[];
var self = this;
$http.get("data.json").then(function (result) {
array = result.data;
init();
function init(){
//all other code will come inside this function
function querySearch(query) {
var results = query ? array.filter(createFilterFor(query)) :array;
return results;
}
}
}
试一试。这就是我在代码中所做的事情。我使用$ scope变量而不是self和this。但它应该和你一起工作。如果发生一些错误,请在评论中告诉我。
<强>更新强>
以下是plunkr:http://plnkr.co/edit/93nzzI?p=preview