离子如何在特定视图加载时调用函数?

时间:2016-04-16 14:28:49

标签: ionic-framework ionic-view

我需要在特定的离子视图中显示/隐藏按钮 这个按钮的外观取决于功能 作为我的样本:

 .controller('IntroCtrl', function ($scope, $state, $ionicSlideBoxDelegate) {

$scope.showalbums=false;
$scope.showalbums_new=true;
 checkfolders();

if(hasalbums==1)
{
    $scope.showalbums=true;
$scope.showalbums_new=false;
}

并在html页面中:

 <i class="ion-images font-ion margin-right-8" ng-click="myAlbums()" ng-show="showalbums"></i>

        <button class="button button-positive button-clear no-animation"
         ng-click="showAlert2()" ng-if="slideIndex == 2" ng-show="showalbums_new" >

和我在js中的方法:

var hasalbums=0;
    function checkfolders()
    {

        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
            function(fileSystem){ // success get file system

                directoryEntry = fileSystem.root;
                if( !directoryEntry.isDirectory ) {
        hasalbums=0;
        }

        currentDir = directoryEntry; // set current directory

        directoryEntry.getParent(function(par){ // success get parent
            parentDir = par; // set parent directory

        }, function(error){ // error get parent
            hasalbums=0;
        });

        var directoryReader = directoryEntry.createReader();
        directoryReader.readEntries(function(entries){


            alert(hasalbums);
            if(entries.length>0)
            {

                hasalbums=1;
            }else{
                hasalbums=0;
            }

        }, function(error){
            hasalbums=0;
        });

            }, function(evt){ // error get file system
                hasalbums=0;
            }
        );
        alert(hasalbums);
    }

但未调用的方法无法显示/隐藏按钮 虽然这个功能正常工作

1 个答案:

答案 0 :(得分:1)

在Ionic中,控制器通常只加载一次。因此,每次到达此视图时,我们都需要使用ionic's生命周期事件来触发控制器中的某些逻辑。例如:

.controller('IntroCtrl', function ($scope, $state, $ionicSlideBoxDelegate) {
  $scope.$on('$ionicView.loaded', function () {
    checkfolders();
  });

现在,在您的内容为checkfolders()后,每次到达此视图时,都会执行loaded方法。请参阅ionichttp://ionicframework.com/docs/api/directive/ionView/

中的其他生命周期事件