我一直在寻找这个问题的答案;我不介意,但实际上它工作得更早。如果在localStorage中没有存储用户访问令牌,我正试图在加载应用程序时在Ionic App中打开一个模态。
这是我正在使用的代码:
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>
正如我之前说的那样,它实际上正在工作,按预期在启动时打开模态。然后它就停止了工作,产生了我之前遇到的错误,没有明显的原因。错误如下:
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">
任何帮助都会被大量赞赏,我无法理解问题所在。
答案 0 :(得分:0)
尝试在$ionicPlatform.ready()
的回调函数中创建模态。
检查此代码:
$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();
};
}
}
在$scope.openModal()
$ionicPlatform.ready()
$ionicPlatform.ready(function(){
...
...
$scope.createModal();
...
...
})
答案 1 :(得分:0)
请尝试打包代码,以便更新$scope
$scope.$apply()
中的$scope.$apply(function () {
<your codes that update $scope.<your variables>>
});
个变量:
$ionicPlatform.ready()
这是因为$scope
中的代码将在另一个JavaScript转弯中运行。 AngularJS不知道$scope.$apply()
变量已更新,因此不会更新任何HTML元素。
在$scope.$digest
中包含您的代码会致电$scope
以将您的@Html.DropDownListFor(m => m.State,
new SelectList(ViewBag.StateList, "StateId", "StateName"),
"Select state",
new { @class = "form-control", @onchange="FillCity()" })
function FillCity() {
var stateId = $('#State').val();
$.ajax({
url: '/Employees/FillCity',
type: "GET",
dataType: "JSON",
data: { State: stateId},
success: function (cities) {
$("#City").html(""); // clear before appending new list
$.each(cities, function (i, city) {
$("#City").append(
$('<option></option>').val(city.CityId).html(city.CityName));
});
}
});
}
变量更改应用于HTML。