user5740661
user5740661

Reputation: 65

ionic how to call function when specific view load?

i need to show /hide buttons at specific ion view and this buttons appearance depend on function as my sample:

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

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

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

and in html page:

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

and my method in 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);
    }

but method not called and can't show/hide buttons as i need while this function is working correctly

Upvotes: 1

Views: 4248

Answers (1)

Aditya Singh
Aditya Singh

Reputation: 16720

In Ionic, controllers normally only load once. So we need to use ionic's lifecycle events to trigger some logic in your controller everytime you arrive at this view. Example:

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

Now your checkfolders() method will be executed everytime you arrive at this view, after your content has loaded. Refer this for other lifecycle events in ionic: http://ionicframework.com/docs/api/directive/ionView/

Upvotes: 2

Related Questions