2016-04-04 16 views
0

Bu sorunun cevabı için her yere bakıyorum; Umrumda olmaz ama aslında daha erken çalışıyordu. LocalStorage'da saklanan kullanıcı erişim belirtecinin bulunmaması durumunda, uygulamanın yükü üzerine İyonik Uygulamada bir model açmaya çalışıyorum. Ben beklendiği gibi aslında başlangıçta modal açmadan önce çalışıyordu söylediği gibi

JS

.controller('LeagueCtrl', function($scope, $ionicModal, $ionicPlatform, $cordovaOauth, Spotify, Favourites) { 
    $scope.favourites = Favourites.all(); 

    $ionicModal.fromTemplateUrl('templates/login.html', { 
    scope: $scope, 
    animation: 'slide-in-up' 
    }).then(function (modal) { 
    $scope.modal = modal; 
    console.log($scope.modal); 
    }); 

    $scope.openModal = function() { 
    $scope.modal.show(); 
    }; 

    $scope.closeModal = function() { 
    $scope.modal.hide(); 
    }; 

... 

    $scope.loginSpotify = function() { 
    $cordovaOauth.spotify('583ac6ce144e4fcb9ae9c29bf9cad5ef', ['user-read-private']).then(function(result) { 
     window.localStorage.setItem('spotify-token', result.access_token); 
     Spotify.setAuthToken(result.access_token); 
     $scope.updateInfo(); 
     //$scope.closeModal(); 
    }, function(error) { 
     console.log("Error -> " + error); 
    }); 
    }; 

    $scope.updateInfo = function() { 
    Spotify.getCurrentUser().then(function(data) { 
     $scope.getUserPlaylists(data.id); 
    }, function(error) { 
     $scope.loginSpotify(); 
    }); 
    }; 

    $ionicPlatform.ready(function() { 
    var storedToken = window.localStorage.getItem('spotify-token'); 
    if (storedToken !== null) { 
     Spotify.setAuthToken(storedToken); 
     $scope.updateInfo(); 
    } else { 
     $scope.openModal(); 
     alert('ionicPlatform ready'); 
    } 
    }); 

}) 

HTML

<html> 
    <head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> 
    <title></title> 


    <link href="css/ionic.app.css" rel="stylesheet"> 

    <script src="lib/ionic/js/ionic.bundle.js"></script> 

    <script src="lib/ngCordova/dist/ng-cordova.js"></script> 
    <script src="lib/ng-cordova-oauth/dist/ng-cordova-oauth.min.js"></script> 
    <script src="lib/angular-spotify/dist/angular-spotify.min.js"></script> 
    <script src="cordova.js"></script> 


    <script src="js/controllers.js"></script> 
    <script src="js/services.js"></script> 
    <script src="js/app.js"></script> 
    </head> 
    <body ng-app="share"> 

    <ion-nav-bar class="bar-assertive"> 
     <ion-nav-back-button> 
     </ion-nav-back-button> 
    </ion-nav-bar> 

    <ion-nav-view></ion-nav-view> 
    </body> 
</html> 

:

Bu

ben çalışıyorum kodudur . Sonra sadece çalışmayı durdurdu ve görünürde bir neden olmadan daha önce karşılaştığım bir hata üretiyordu.

Herhangi bir yardım kitlesel mutluluk duyacağız
ypeError: Cannot read property 'show' of undefined 
at Scope.$scope.openModal (http://localhost:8100/js/controllers.js:24:17) 
at http://localhost:8100/js/controllers.js:60:14 
at http://localhost:8100/lib/ionic/js/ionic.bundle.js:53329:19 
at Object.ionic.Platform.ready (http://localhost:8100/lib/ionic/js/ionic.bundle.js:2135:9) 
at Object.self.ready (http://localhost:8100/lib/ionic/js/ionic.bundle.js:53327:26) 
at new <anonymous> (http://localhost:8100/js/controllers.js:54:18) 
at invoke (http://localhost:8100/lib/ionic/js/ionic.bundle.js:17762:17) 
at Object.instantiate (http://localhost:8100/lib/ionic/js/ionic.bundle.js:17770:27) 
at http://localhost:8100/lib/ionic/js/ionic.bundle.js:22326:28 
at self.appendViewElement (http://localhost:8100/lib/ionic/js/ionic.bundle.js:56883:24) <ion-nav-view name="tab-league" class="view-container tab-content" nav-view="active" nav-view-transition="ios"> 

, sorunun ne olduğunu kavrayamayan şöyledir: hatadır.

cevap

0

$ionicPlatform.ready() numaralı geri arama işlevinde model oluşturmayı deneyin.

Kontrol Bu kod: $ionicPlatform.ready()

$ionicPlatform.ready(function(){ 
    ... 
    ... 
    $scope.createModal(); 
    ... 
    ... 
}) 
0

içinde $scope.openModal() yerine fonksiyonu üzerinde

$scope.createModal = function(){ 
    if($scope.modal){ 
     $scope.openModal(); 
    } 
    else{ 
     $ionicModal.fromTemplateUrl('templates/login.html', { 
      scope: $scope, 
      animation: 'slide-in-up' 
     }).then(function (modal) { 
      $scope.modal = modal; 
      $scope.modal.show(); 
     }); 

     $scope.openModal = function() { 
      $scope.modal.show(); 
     }; 

     $scope.closeModal = function() { 
      $scope.modal.hide(); 
     }; 
    } 
} 

Çağrı böyle $scope.$apply() yılında $scope değişkenler güncellenir senin kodlarını sarmak için deneyin:

$scope.$apply(function() { 
    <your codes that update $scope.<your variables>> 
}); 

Çünkü $ionicPlatform.ready()'daki kodlar başka bir JavaScript dönüşünde çalıştırılacak. AngularJS, $scope değişkenlerinin güncellendiğini bilmiyor ve bu nedenle hiçbir HTML öğesi güncellenmeyecek.

$scope.$apply() kodlarınızı kodlamak, $scope değişken değişikliklerinizi HTML'ye uygulamak için $scope.$digest numaralı telefonu arayacaktır.