【问题标题】:Ionic typeahead with ionic-filter-bar - cannot reference $scope离子过滤条的离子预输入 - 无法引用 $scope
【发布时间】:2017-03-13 16:31:54
【问题描述】:

我正在尝试实现 ionic-filter-bar,以便在我的 ionic 移动应用程序上进行预输入,并且在尝试确定控制器中的 $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(请参阅代码中的 cmets)。我认为这与 $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);
    };


})

啤酒花列表页面

<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>

【问题讨论】:

    标签: javascript angularjs ionic-framework ionic ionic-filter-bar


    【解决方案1】:

    您需要使用 items 属性,而不是跳入过滤栏服务。所以你对 showFilterBare 的调用应该是

    filterBarInstance = $ionicFilterBar.show({
    
            items: $scope.hops,
            update: function(filteredItems) {
                $scope.hops = filteredItems;
    
                //This returns "undefined"
                console.log(filteredItems);
            },
            filterProperties: 'Name'
        });
    

    【讨论】:

      【解决方案2】:

      当一个 Promise 被成功解决(即异步)时,您正在设置 $scope.hops。基本上,在承诺解决之前,您正在执行 console.log 并且范围内没有跳跃。网上有一种关于承诺的信息。我个人喜欢这样的解释:https://stackoverflow.com/a/15738351/3927379

      【讨论】:

      • 感谢您的回复。我想我现在更好地理解了承诺,并且肯定知道我哪里出错了,但是对于我的生活,我似乎无法解决它。你有什么建议吗?
      • 对不起,我没用过$ionicFilterBar。所以我不能说你的那部分代码
      【解决方案3】:

      替换

      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'
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-01
        • 2021-06-19
        • 2018-03-11
        • 1970-01-01
        • 2018-03-24
        • 2016-12-27
        • 2021-07-05
        • 1970-01-01
        相关资源
        最近更新 更多