搜尋

首頁  >  問答  >  主體

angular.js - AngularJS中,一個頁面跳到它自己,該怎麼寫

寫一個資料夾查看頁,那麼查看其子資料夾,必然還是要跳到資料夾查看頁,可是路由只有在url發生改變之時才會觸發跳轉,我試過了在資料夾查看頁的路由對應的url加入一個隨機數的參數,可是這種方法根本不起作用,後來做成了點擊資料夾後只是改變資料夾列表數組的值,不用跳轉,可是這樣也不能觸發頁面自動刷新,所以我只能用$window.location.reload(true)這種方法,手動刷新,但是其效果很糟糕,有時候起作用,有時候不起作用,求大神幫忙。
下面是路由,controller,和頁面路由

.state('tab.folder-list', {
    url: '/folder-list',
    views: {
        'tab-more': {
            templateUrl: "templates/work/folder/folder-list.html",
            controller: "folderCtrl"
        }
    }
})

對應的controller頁面

appCtrls.controller('folderCtrl', function ($scope, $state, $window, goBackService, $ionicActionSheet, folderService, $stateParams, localStorageService) {

    var isr = false;
    $scope.getAllFolderList = function () {
        var pro = folderService.isRootFolder(localStorageService.getLocalStorage("current").currentFolderId);
        pro.then(function (res) {
            $scope.isRoot = res.data;
            isr = $scope.isRoot;
        })
        var promise = folderService.getAllFolderList(localStorageService.getLocalStorage("current").currentFolderId);
        promise.then(function (response) {
                console.debug("List----->>>" + angular.toJson(response.data));
                $scope.folderList = response.data.folderList;
                $scope.fileList = response.data.fileList;
                localStorageService.setLocalStorage("currentFolderList",$scope.folderList);
            }, function (response) {
                $scope.folderList = [];
                $scope.fileList = [];
            }
        )
        //$window.location.reload(true);
    }
    //currentFolderId:当前目录Id,previousFolderId:当前目录的上级目录id,用于返回到上级目录
    //当点击文件夹列表中的文件夹时,会将点击的该文件夹id(跳转后变成当前目录id),以及跳转前的当前目录id(跳转后就变成了上一级目录id)传过去
    //根目录与其他目录不同之处,在于根目录不允许删除,编辑
    $scope.goToViewFolder = function (folder) {
        var current = {
            currentFolderId: folder.id,
            previousFolderId: localStorageService.getLocalStorage("current").currentFolderId
        };
        localStorageService.setLocalStorage("current", current);

        $scope.isRoot = false;
        var promise = folderService.getAllFolderList(localStorageService.getLocalStorage("current").currentFolderId);
        promise.then(function (response) {
                $scope.folderList = response.data.folderList;
                $scope.fileList = response.data.fileList;
                localStorageService.setLocalStorage("currentFolderList",$scope.folderList);
            }, function (response) {
                $scope.folderList = [];
                $scope.fileList = [];
            }
        )
        $window.location.reload(true);
        /*var now=new Date();
         var number = now.getSeconds();
         $state.go("tab.folder-list",{random:number},{reload: true});*/
    }


    $scope.showAdd = function (folder) {
        var menuItems = [
            {text: "新建文件"},
            {text: "新建文件夹"}
        ];

        // Show the action sheet
        var hideSheet = $ionicActionSheet.show({
            titleText: "",
            buttons: menuItems,
            buttonClicked: function (index) {
                if (index == 1) {
                    $state.go("tab.folder-add");
                } else {
                    $state.go("tab.file-chooseWayToGainFile");
                }

            },
            cancelText: "取消",
            cancel: function () {
                // add cancel code..
            }
            /*,
             destructiveText: "删除",
             destructiveButtonClicked:function(){
             }*/
        });
    };
    //返回上级目录,刷新列表页
    $scope.backToPrevious = function () {
        var previousFolderId = "";
        //console.debug("previousFolderId-->>"+localStorageService.getLocalStorage("current").previousFolderId);
        var promise = folderService.getParentFolderId(localStorageService.getLocalStorage("current").previousFolderId);
        promise.then(function (res) {
            previousFolderId = res.data;
            var current = {
                currentFolderId: localStorageService.getLocalStorage("current").previousFolderId,
                previousFolderId: previousFolderId
            };
            localStorageService.setLocalStorage("current", current);
        })

        var pro = folderService.isRootFolder(localStorageService.getLocalStorage("current").currentFolderId);
        pro.then(function (res) {
            $scope.isRoot = res.data;
            isr = $scope.isRoot;
        })
        var promise = folderService.getAllFolderList(localStorageService.getLocalStorage("current").currentFolderId);
        promise.then(function (response) {
                $scope.folderList = response.data.folderList;
                $scope.fileList = response.data.fileList;
                localStorageService.setLocalStorage("currentFolderList",$scope.folderList);
            }, function (response) {
                $scope.folderList = [];
                $scope.fileList = [];
            }
        )
        $window.location.reload(true);
        /*var now=new Date();
         var number = now.getSeconds();
         $state.go("tab.folder-list",{random:number},{reload: true});*/
    }
    $scope.goToViewFile = function (file) {
        $state.go("tab.file-fileView", {fileId: file.id});
    }
    $scope.editFolder = function () {
        $state.go("tab.folder-edit");
    }

});
<ion-view view-title="列表">
    <ion-nav-buttons side="right">
        <a class="button button-clear" ng-click="editFolder()" ng-show=""><i class="icon ion-ios-compose-outline"></i></a>
        <a class="button button-clear" ng-click="showAdd()"><i class="icon ion-android-menu"></i></a>
    </ion-nav-buttons>
    <ion-nav-buttons side="left">
        <a ng-show="isRoot" class="button button-clear" ui-sref="tab.file-companysIBelongTo"><i class="icon ion-ios-arrow-left"></i>公司列表</a>
        <a ng-show="!isRoot" class="button button-clear" ng-click="backToPrevious()"><i class="icon ion-ios-arrow-left"></i>返回上级目录</a>
    </ion-nav-buttons>
    <ion-content class="no-padding"  ng-init="getAllFolderList()">
        <p class="item item-pider">文件夹列表</p>
        <p class="list">
            <p class="item item-icon-left item-icon-right" ng-repeat="folder in folderList" ng-click="goToViewFolder(folder)">
                <i class="icon ion-android-folder"></i>
                {{folder.name}}
                <i class="icon ion-chevron-right icon-accessory"></i>
            </p>

        </p>
        <p class="item item-pider">文件列表</p>
        <p class="list">
            <p class="item item-icon-left item-icon-right" ng-repeat="file in fileList" ng-click="goToViewFile(file)">
                <i class="icon ion-android-document"></i>
                {{file.name}}
                <i class="icon ion-chevron-right icon-accessory"></i>
            </p>
        </p>
    </ion-content>
</ion-view>
大家讲道理大家讲道理2744 天前520

全部回覆(1)我來回復

  • 大家讲道理

    大家讲道理2017-05-15 16:58:05

    http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.directive:ui-sref
    用ui-sref-opts參數

    <p ui-sref="{string}" ui-sref-opts="{reload:true}">
       ...
    </p>

    回覆
    0
  • 取消回覆