【问题标题】:Can't update ion-slide on ajax success无法在 ajax 成功时更新 ion-slide
【发布时间】:2015-01-12 18:55:52
【问题描述】:

我在更新我的 ionic 框架幻灯片时遇到了问题。我整理了一个jsfiddle,它是目前的形式,没有功能,但是,你可以在那里看到代码。

我的问题是,如果仅出于测试目的,我在加载页面时使用 ajax,它工作得很好。如果我使用超时来模拟异步,它也可以工作。

如果我在 ngSubmit 上设置了一个函数,则幻灯片不会更新,但是,ajax 的成功部分会检索数据。我也可以在html端打印,但是幻灯片没有更新。

谁能告诉我为什么?

HTML:

<ion-view view-title="Search" ng-controller="UserSearchCtrl" class="title-left">
    <ion-content scroll="false">

        <div class="list">
            <form ng-submit="retrieveResults()" ng-controller="UserSearchCtrl">
                <input type="text" placeholder="Search" ng-model="criteria">
            </form>
        </div>

        <div class="search-results">
            <ion-slide-box show-pager="false">
                <ion-slide ng-repeat="user in result.users" class="img-box-6">
                    {{user}}
                </ion-slide>
            </ion-slide-box>
        </div>

    </ion-content>
</ion-view>

JavaScript,不工作:

engine.module.controller('UserSearchCtrl', function($scope, $rootScope, $http, $ionicSlideBoxDelegate, $timeout, $state, $stateParams, $db, $user) {

    $ionicSlideBoxDelegate.enableSlide(false);
    $scope.width = window.outerWidth;

    // if I wrap the below ajax fn in a function, called on submit, it stops working
    $scope.retrieveResults = function() {
        var token = $rootScope.user.token;

        if ($scope.criteria !== '') {

            $http.get(engine.config.REMOTE_URL + '/search?' + 'token=' + token) 
                .success(function(response) {
                    $scope.result = response.data;

                    // debug - they both alert the same thing
                    //alert(response.data.search_type);
                    //alert($scope.result.search_type);

                    $ionicSlideBoxDelegate.enableSlide(true);
                    $ionicSlideBoxDelegate.update();
                })

        } else {
            // ALERT: criteria is not set
        }

    };
});

JavaScript,无需 ngSubmit 即可工作:

engine.module.controller('UserSearchCtrl', function($scope, $rootScope, $http, $ionicSlideBoxDelegate, $timeout, $state, $stateParams, $db, $user) {

    $ionicSlideBoxDelegate.enableSlide(false);
    $scope.width = window.outerWidth;

    //$scope.retrieveResults = function() {
        var token = $rootScope.user.token;

        if ($scope.criteria !== '') {

            $http.get(engine.config.REMOTE_URL + '/search?' + 'token=' + token) 
                .success(function(response) {
                    $scope.result = response.data;

                    $ionicSlideBoxDelegate.enableSlide(true);
                    $ionicSlideBoxDelegate.update();
                })

        } else {
            // ALERT: criteria is not set
        }

    //};
});

更新 如果我在点击时调用该函数,它也可以工作。似乎只有在提交调用更新它的函数时幻灯片才起作用..

【问题讨论】:

  • 解决了吗,或者你需要什么东西吗?

标签: ajax angularjs angularjs-ng-repeat ionic-framework


【解决方案1】:

因为它是异步的,我认为您的 $ionicSlideBoxDelegate 与您的控制器中的不同。您可以执行以下操作:

engine.module.controller('UserSearchCtrl', function($scope, $rootScope, $http, $ionicSlideBoxDelegate, $timeout, $state, $stateParams, $db, $user) {

    var currentIonicSlideBoxDelegate = $ionicSlideBoxDelegate;
    currentIonicSlideBoxDelegate.enableSlide(false);
    $scope.width = window.outerWidth;

    // if I wrap the below ajax fn in a function, called on submit, it stops working
    $scope.retrieveResults = function() {
        var token = $rootScope.user.token;

        if ($scope.criteria !== '') {

            $http.get(engine.config.REMOTE_URL + '/search?' + 'token=' + token) 
                .error(function(err) {
                    // ERROR: HTTP GET
                })
                .success(function(response) {
                    $scope.result = response.data;

                    // debug - they both alert the same thing
                    //alert(response.data.search_type);
                    //alert($scope.result.search_type);

                    currentIonicSlideBoxDelegate.enableSlide(true);
                    currentIonicSlideBoxDelegate.update();
                })
                .finally(function() {
                    //
                });

        } else {
            // ALERT: criteria is not set
        }

    };
});

你的 jsFiddler 不工作,所以我无法测试这个,但我认为它会工作。

【讨论】:

  • 抱歉,我不确定是否可以发布这些 URL,而且我没有测试 URL 可以替代使用。
  • 我也尝试过使用 $scope.$apply(function(){ });无论是设置变量还是更新,它总是失败。出于这个原因,我认为没有异步问题..
  • 网页浏览器的控制台没有报错吗?
  • 不,没什么。通过警报,我可以获得结果。我可以将结果写在 html 中的任何位置。我不能做的是更新幻灯片..
  • 我已经更新了帖子。如果该函数在点击时调用,它也可以工作。
【解决方案2】:

找到了!这是我的错误。同一个控制器内部还有一个控制器。

ng-controller="UserSearchCtrl"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 2016-08-24
    相关资源
    最近更新 更多