简体   繁体   English

State.go()显示地址页面中的url但不刷新html视图

[英]State.go() shows the url in address page but doesn't refresh the html view

I have a problem when whorking on Ionic with angularJs, the problem is in routing system when I try to develop a login page . 当我使用angularJs在Ionic上进行操作时遇到问题,当我尝试开发登录页面时问题出现在路由系统中。 In the controller part of code i'l trying to load a welcome page calle 'dash' with state.go(psc.dash) 在代码的控制器部分,我试图用state.go(psc.dash)加载一个欢迎页面calle' dash'

here is my controller.js : 这是我的controller.js

angular.module('starter.controllers', [])
    .controller('LoginCtrl', function($location, $state) {
        var user = this;

        user.login = function() {
            console.log("LOGIN user: " + user.email + " - PW: " + user.password);
            setTimeout(function() {
                state.go('psc.dash');
            }, 20);
        };
    })
   .controller('DashCtrl', function($scope, $location) {});

here is my App.js: 这是我的App.js:

.config(function($stateProvider, $urlRouterProvider, $locationProvider) {
  $stateProvider

  .state('login', {
    url: "/login",
    views: {
      'login': {
        templateUrl: "templates/login.html",
        controller: "LoginCtrl"
      }
    }
  })
  .state('psc', {
    url: "/psc",
    abstract: true,
    templateUrl: "templates/psc.html"
  })
  .state('psc.dash', {
    url: "/dash",
    views: {
      'psc-dash': {
        templateUrl: "templates/dash.html",
        controller: "DashCtrl"
      }
    }
  })
  ;

  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/login');    
});

and here is my login.html 这是我的login.html

<div class="list list col span_1_of_2 " ng-controller="LoginCtrl as loginCtrl">
    <label class="item item-input">
        <span class="input-label">E-mail</span>
        <input type="text" ng-model="loginCtrl.email">
    </label>
    <label class="item item-input">
        <span class="input-label">password</span>
        <input type="password" ng-model="loginCtrl.password">
    </label>
    <div>
        <div class="col span_2_of_3"><a href="  ">forgot password ? </a></div>
        <button class="button button-positive  col span_1_of_3" ng-click="loginCtrl.login()">
            connect
        </button>
    </div>
</div>

The problem is when I click on connect button the url '/psc/dash' appears in address bar but the login view stay displayed and the page is not reloaded with the new html view. 问题是,当我点击连接按钮时,地址栏中会显示网址“/ psc / dash”,但登录视图仍会显示,并且不会使用新的html视图重新加载页面。

EDIT 编辑

This is wrong. 这是错的。 There is a discrepancy in the ui-router documentation: states that inherit from an abstract state are prefixed with their parents URL . ui-router文档中存在差异: 从抽象状态继承的状态以其父URL开头


The reason is that although your 'psc.dash' nested state is defined as a child of the 'psc' state, the URL you have assigned to it is not automatically prefixed with its parent's URL. 原因是虽然您的'psc.dash'嵌套状态被定义为'psc'状态的子级,但您分配给它的URL不会自动以其父级URL作为前缀。

You want to change the 'psc.dash' state definition to this: 您想要将'psc.dash'状态定义更改为:

.state('psc.dash', {
    url: "/psc/dash",
    views: {
      'psc-dash': {
        templateUrl: "templates/dash.html",
        controller: "DashCtrl"
      }
    }
  })

Take a look at the ui-router documentation for why this is: 请查看ui-router文档,了解其原因:

What Do Child States Inherit From Parent States? 儿童国家对父母国家的继承是什么?

Child states DO inherit the following from parent states: 子状态DO从父状态继承以下内容:

Nothing else is inherited (no controllers, templates, url, etc). 没有其他东西被继承(没有控制器,模板,网址等)。

The service is $state so the code should be: 该服务是$state所以代码应该是:

$state.go('psc.dash');

You can get rid of the controller definition in your HTML: 您可以删除HTML中的控制器定义:

<div class="list list col span_1_of_2" ng-controller="LoginCtrl as loginCtrl">

</div>

use this instead: 改用它:

<div class="list list col span_1_of_2">

</div>

and change the state this way: 并以这种方式改变状态:

.state('login', {
    url: "/login",
    views: {
      'login': {
        templateUrl: "templates/login.html",
        controller: "LoginCtrl as loginCtrl"
      }
    }
  })

You don't need to define the controller twice. 您不需要两次定义控制器。

Your login.html template does not use <ion-view> . 您的login.html模板不使用<ion-view> It would be interesting to see a plunker of your project. 看到你的项目的一个东西会很有趣。

Another things I am not sure about is the state definition. 我不确定的另一件事是州定义。 If the view login is not wrapped in another container you should write it like this: 如果视图登录未包装在另一个容器中,您应该像这样写:

.state('login', {
    url: "/login",
    templateUrl: "templates/login.html",
    controller: "LoginCtrl as loginCtrl"
  })

SOLUTION: my psc route should be named 'login' to be identified 解决方案:我的psc路线应该被命名为'login'来识别

  .state('psc', {
url: "/psc",
 views: {
  'login': {
      abstract: true,
   templateUrl: "templates/psc.html"
  }
}
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM