首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何正确地从另一个视图使用$state.go重新初始化视图

如何正确地从另一个视图使用$state.go重新初始化视图
EN

Stack Overflow用户
提问于 2019-04-17 01:56:45
回答 2查看 48关注 0票数 0

我尝试使用此方法将用户从一个状态发送到另一个状态:

代码语言:javascript
复制
$state.go('splash');

这是正确的,但是,当用户被发送到我的splash视图时,代码不会重新启动。

这是启动状态的html:

代码语言:javascript
复制
<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>

这是视图的控制器:

代码语言:javascript
复制
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定向到那里时-模态不加载。

有什么想法让我能让它正常工作吗?

EN

回答 2

Stack Overflow用户

发布于 2019-04-30 12:45:04

我希望当用户被路由到“启动”状态时,你正在寻找重新加载你的“SplashCtrl”控制器。如果是,试试这个$state.go('splash',{},{reload: true});

票数 0
EN

Stack Overflow用户

发布于 2019-10-10 21:23:15

你还没有分享你的整个项目,所以我真的不能说。看起来你的结构有点不对劲。您应该只有一个ionicPlatform.ready(),并且它应该用于设置环境。一旦完成,它应该把用户转到你的第一个视图,而不是打开一个模式。

这就是我在我的应用程序中检查的方式。平台就绪函数有$state.go('app.calendar');这是我的第一个视图(假设他们已经登录)-如果他们没有登录,init函数会从promise返回一个错误,状态更改错误代码会将他们转发到登录注册页面。

状态控制器

代码语言:javascript
复制
.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'
        }
    }
})

检查是否已登录。

代码语言:javascript
复制
function init(){
    var deferred = $q.defer();
    if (loggedIn()) {
        deferred.resolve(currentUser);
    } else {
        deferred.reject({error: "noUser"});
    }
    return deferred.promise;
};

对状态error - app-first的反应是登录或注册页面。

代码语言:javascript
复制
$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', {});
        }
    }
);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55714058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档