我尝试使用此方法将用户从一个状态发送到另一个状态:
$state.go('splash');这是正确的,但是,当用户被发送到我的splash视图时,代码不会重新启动。
这是启动状态的html:
<ion-view hide-nav-bar="true">
<ion-content class="splash-screen" scroll="false">
<div class="loading">
<i class="fa fa-circle-o-notch fa-spin fa-5x fa-fw"></i>
</div>
</ion-content>
</ion-view>
<script id="login.html" type="text/ng-template">
<ion-modal-view>
<ion-header-bar class="bar bar-header bar-stable">
<h1 class="title">Login</h1>
</ion-header-bar>
<ion-content>
<div class="list padding">
<div class="error" ng-if="errors">
<p ng-bind-html="error_message"></p>
</div>
<label class="item item-input">
<input ng-model="data.password" type="password" placeholder="Password">
</label>
<button class="button button-full button-positive" ng-click="login(data)">Login</button>
</div>
</ion-content>
</ion-modal-view>
</script>这是视图的控制器:
angular.module('app.controllers', [])
.controller('SplashCtrl', function($scope, $http, $state, $ionicModal, $ionicLoading, $ionicPlatform, $timeout, $localstorage, appAPI) {
$scope.data = {};
$scope.data.password = '';
$ionicModal.fromTemplateUrl("login.html", {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.modal = modal;
});
$scope.openModal = function() {
$scope.modal.show();
};
$scope.login = function(data) {
alert(data.password);
};
$ionicPlatform.ready(function() {
$scope.openModal();
});
});当用户第一次访问splash视图时,模态加载是预期的,但是,当它们从$state.go定向到那里时-模态不加载。
有什么想法让我能让它正常工作吗?
发布于 2019-10-10 21:23:15
你还没有分享你的整个项目,所以我真的不能说。看起来你的结构有点不对劲。您应该只有一个ionicPlatform.ready(),并且它应该用于设置环境。一旦完成,它应该把用户转到你的第一个视图,而不是打开一个模式。
这就是我在我的应用程序中检查的方式。平台就绪函数有$state.go('app.calendar');这是我的第一个视图(假设他们已经登录)-如果他们没有登录,init函数会从promise返回一个错误,状态更改错误代码会将他们转发到登录注册页面。
状态控制器
.state('app', {
url: '/app',
cache: false,
abstract: true,
templateUrl: 'templates/tabs.html',
controller: 'TabCtrl',
resolve: {
user: function (UserService) {
var value = UserService.init();
return value;
}
}
})
// Calendar
.state('app.calendar', {
url: "/calendar",
cache: true,
views: {
'calendar-tab': {
templateUrl: 'templates/calendar.html',
controller: 'CalendarController'
}
}
})检查是否已登录。
function init(){
var deferred = $q.defer();
if (loggedIn()) {
deferred.resolve(currentUser);
} else {
deferred.reject({error: "noUser"});
}
return deferred.promise;
};对状态error - app-first的反应是登录或注册页面。
$rootScope.$on('$stateChangeError',
function (event, toState, toParams, fromState, fromParams, error) {
console.log('$stateChangeError ' + error && (error.debug || error.message || error));
// if the error is "noUser" the go to login state
if (error && error.error === "noUser") {
event.preventDefault();
$state.go('app-first', {});
}
}
);https://stackoverflow.com/questions/55714058
复制相似问题