gpt4 book ai didi

javascript - Angular $stateProvider 未按预期路由

转载 作者:行者123 更新时间:2023-12-02 14:08:42 27 4
gpt4 key购买 nike

在我的主 index.html 文件中,我有以下简单标记...

<body ng-app="starter" ng-controller="AppCtrl">
<div ui-view></div>
</body>

在我的 app.js 中,我使用 $stateProvider 创建路由,以便我可以显示某些页面...

app.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/app');
$stateProvider

.state('app', {
url: '/app',
templateUrl: 'templates/menu.html',
controller: 'AppCtrl'
})
.state('app.accounts', {
url: '/accounts',
templateUrl: 'templates/accounts.html'
})
});

当页面加载时,第一个状态被加载,这意味着我可以在我的主 index.html 和 Controller 中看到 menu.html 的内容AppCtrl 被传递到此状态。

我的 AppCtrl 加载一个 API,我在单击 menu.html 中的按钮时使用该 API,该 API 为用户提供了一个用于登录的 UI,一旦凭证良好,成功称为...

app.controller('AppCtrl', function($scope, $ionicModal, $timeout, $state) {

$scope.create = function() {
var linkHandler = Plaid.create({
env: 'tartan',
clientName: 'Example Project',
key: 'test_key',
product: 'connect',
onSuccess: function(token) {
$state.go('app.accounts');
},
});

linkHandler.open();
}
});

API 的作用非常无关紧要,但正如您所看到的,我在成功时传递 $state.go('app.accounts'); 。但我相信调用了 otherwise 语句,而不是将状态更改为 app.accounts ,因为我看到的只是 app 状态的内容.

为什么会这样呢?我已经被这个问题困扰有一段时间了。

最佳答案

app.accountsapp 的子状态。这意味着 menu.html一定有<ui-view>为了显示accounts.html .

如果您不想显示accounts.html里面menu.html ,你不应该做 accounts app 的子状态:

<body ng-app="starter">
<div ui-view></div>
</body>

app.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/app');
$stateProvider

.state('app', {
url: '/app',
templateUrl: 'templates/menu.html',
controller: 'AppCtrl'
})
.state('accounts', {
url: '/accounts',
templateUrl: 'templates/accounts.html'
})
});

关于javascript - Angular $stateProvider 未按预期路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39813103/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com