我正在尝试实现离子过滤器条,以便在我的离子移动应用程序上使用typeahead工作,并且在尝试确定控制器中的$ scope时遇到问题。我一直在复制这里的演示应用程序中的大部分代码:
https://github.com/djett41/ionic-filter-bar/blob/master/demo/app/scripts/app.js
我可以确定$ scope.hops,以便在页面上正确呈现的ng-repeat中正确显示所有对象(无需搜索),但我无法在showFilterBar函数中引用$ scope.hops。输出到console.log时我收到undefined(请参阅代码中的注释)。我认为这与在其他函数中访问$ scope的方式有关(??)但是卡住了。
任何人都可以帮我一把吗?提前谢谢。
控制器
//Added $ionicFilterBar & $timeout - Remove if does not work
.controller("hopsListCtrl", function($scope, hopsService, $stateParams, $state, $ionicFilterBar, $timeout) {
hopsService.getHops().then(function(hops) {
//This correctly returns my object of hop varieties
$scope.hops = hops;
})
$scope.onSelectHopsDetail = function(hops_detail) {
$state.go('tab.hopsDetail', hops_detail);
hopsService.postHopDetails(hops_detail);
};
var filterBarInstance;
//This returns "undefined" -
console.log($scope.hops);
$scope.showFilterBar = function() {
filterBarInstance = $ionicFilterBar.show({
hops: $scope.hops,
update: function(filteredItems) {
$scope.hops = filteredItems;
//This returns "undefined"
console.log(filteredItems);
},
filterProperties: 'Name'
});
};
$scope.refreshItems = function() {
if (filterBarInstance) {
filterBarInstance();
filterBarInstance = null;
}
$timeout(function() {
getItems();
$scope.$broadcast('scroll.refreshComplete');
}, 1000);
};
})
Hops列表页面
<ion-view title="Hops Guide">
<ion-nav-buttons side="secondary">
<button class="button-icon icon ion-ios-search" ng-click="showFilterBar()">
</button>
</ion-nav-buttons>
<ion-content direction="y" scrollbar-y="false">
<ion-refresher pulling-icon="ion-arrow-down-b" on-refresh="refreshItems()">
</ion-refresher>
<ion-list>
<ion-item collection-repeat="hop in hops">
<a class="" ng-click="onSelectHopsDetail(hop)">
<p>{{hop.Name}}</p>
</ion-item>
</ion-list>
<div ng-if="hops !== undefined && !hops.length" class="no-results">
<p>No Results</p>
</div>
</ion-content>
</ion-view>
答案 0 :(得分:1)
您需要使用items属性而不是跳转到过滤器栏服务。所以你对showFilterBare的调用应该是
filterBarInstance = $ionicFilterBar.show({
items: $scope.hops,
update: function(filteredItems) {
$scope.hops = filteredItems;
//This returns "undefined"
console.log(filteredItems);
},
filterProperties: 'Name'
});
答案 1 :(得分:0)
在成功解决承诺(即异步)时,您正在设置$scope.hops
。基本上,您在承诺解决之前执行了console.log,并且范围没有跳跃。关于网上承诺的信息基调。我个人喜欢这样的解释:https://stackoverflow.com/a/15738351/3927379
答案 2 :(得分:0)
替换
filterBarInstance = $ionicFilterBar.show({
items: $scope.hops,
update: function(filteredItems) {
$scope.hops = filteredItems;
//This returns "undefined"
console.log(filteredItems);
},
filterProperties: 'Name'
});
与
filterBarInstance = $ionicFilterBar.show({
items: $scope.hops,
update: function(filteredItems, filterText) {
$scope.hops = filteredItems;
//This returns "undefined"
console.log(filteredItems);
},
filterProperties: 'Name'
});