State.go()在地址页中显示url,但不刷新html视图
State.go() shows the url in address page but doesn't refresh the html view
我在使用angularJs工作Ionic时遇到了一个问题,问题是在我尝试开发登录页面时路由系统。在代码的控制器部分,我将尝试加载一个名为"破折号"的欢迎页面与state.go(psc.dash)
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) {});
这是我的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');
});
这里是我的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>
问题是当我点击连接按钮url '/psc/破折号'出现在地址栏中,但登录视图保持显示,页面没有重新加载新的html视图。
EDIT
这是错误的。在ui-router
文档中有一个差异:从抽象状态继承的状态以其父URL为前缀。
原因是,尽管您的'psc.dash'
嵌套状态被定义为'psc'
状态的子状态,您分配给它的URL不会自动添加其父URL的前缀。
您想将'psc.dash'
状态定义更改为:
.state('psc.dash', {
url: "/psc/dash",
views: {
'psc-dash': {
templateUrl: "templates/dash.html",
controller: "DashCtrl"
}
}
})
查看ui-router文档,了解为什么会这样:
子状态从父状态继承了什么?
子状态继承父状态:
通过解析自定义数据属性解析依赖项
自定义
data
属性没有继承其他任何东西(没有控制器、模板、url等)。
服务是$state
,所以代码应该是:
$state.go('psc.dash');
你可以去掉HTML中的控制器定义:
<div class="list list col span_1_of_2" ng-controller="LoginCtrl as loginCtrl">
</div>
用这个代替:
<div class="list list col span_1_of_2">
</div>
,这样改变状态:
.state('login', {
url: "/login",
views: {
'login': {
templateUrl: "templates/login.html",
controller: "LoginCtrl as loginCtrl"
}
}
})
你不需要定义两次控制器。
你的login.html模板没有使用<ion-view>
。看一下你的项目的概要会很有趣。
另一件我不确定的事情是状态定义。如果view login没有包装在另一个容器中,你应该这样写:
.state('login', {
url: "/login",
templateUrl: "templates/login.html",
controller: "LoginCtrl as loginCtrl"
})
解决方案:我的PSC路由应该命名为"login"以进行识别
.state('psc', {
url: "/psc",
views: {
'login': {
abstract: true,
templateUrl: "templates/psc.html"
}
}
})
相关文章:
- 刷新后重定向到其他URL
- window.location未刷新到现有URL
- 在浏览器中更改页面的URL,无需刷新和HTML5模式
- Angular/UI路由器-如何在不刷新所有内容的情况下更新URL
- Angular,ui路由器;绑定”;到url,也不刷新
- 如何在不刷新页面的情况下更改URL
- dojo-dgrid带有store from url(json数据)重载/刷新按钮
- 更新f5上的URL或刷新按钮
- 在url中传递参数时,如何使用AJAX阻止页面刷新
- PHP-如何让别人可以'不要更改url中的参数,也不要在刷新页面时再次运行脚本
- 用javascript附加主url,刷新页面无效
- 如果可能的话,刷新后保留iframe url
- 剑道自动完成刷新url
- 对于特定的URL,每5秒刷新一次表
- 用javascript交换url后刷新类似facebook的按钮不起作用
- 使用jQuery修改url参数而不刷新页面
- 单击具有实际 URL 的链接时刷新
- 角度 UI 路由器嵌套视图 使用或刷新直接 URL 时不会加载页面
- 如何使用 url 哈希在刷新后加载特定页面
- 任何想法如何重定向一个页面到登录页面时,基于它的url刷新