angular $stateProvider not routing as expected

不羁的心 提交于 2019-12-25 09:17:11

问题


In my main index.html file I have the following simple markup...

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

In my app.js I am using $stateProvider to create routes so I can display certain pages...

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'
    })
});

When the page loads, the first state is loaded, meaning I can see the contents of menu.html in my main index.html and the controller AppCtrl is passed to this state.

My AppCtrl loads an API that I am using on click of a button from menu.html, the API provides a UI for a user to login, and once the credentials are good, the success is called...

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();
  }   
});

What the API does is pretty irrelevant, but as you can see, I am passing $state.go('app.accounts'); on success. But instead of changing the state to app.accounts, I believe the otherwise statement is called because all I see is the contents of the app state.

Why is this so? I've been stuck on this issue for some time now.


回答1:


app.accounts is a child state of app. That means in menu.html there must be <ui-view> in order to display accounts.html.

If you don't want to display accounts.html inside menu.html, you shouldn't make accounts a child state of app:

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

and

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'
    })
});


来源:https://stackoverflow.com/questions/39813103/angular-stateprovider-not-routing-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!