简体   繁体   中英

ionic $state.go not loading page or showing error

I'm using $state.go() other places in my app but it's not working in a simple function and I can't work out why. It's not showing an error or showing the right state called in $state.go().

I've tested it with $stateChangeStart, $stateChangeError, $viewContentLoading & $stateChangeSuccess. It reaches all the correct stages: $stateChangeStart, $viewContentLoading then $stateChangeSuccess with the right content in each however, doesn't load the page.

I originally tried it with ui-sref and that didn't work so I tried it with $state.go() and now that isn't working I'm really confused. I can't see any difference between this code and what I have in other places where $state.go() works.

HTML link:

<li ng-repeat="thing in things" class="item" ng-click="showThingDetails()">

Code in controller:

$scope.showThingDetails = function () {
$state.go('thingDetails');}

Code in state:

.state('thingDetails', {
url: '/thingDetails',
views: {
  'menuContent': {
    templateUrl: 'templates/thingDetails.html',
    controller: 'thingDetails'
  }
}
})

thingDetails.html

<ion-view view-title="Thing Details">
  <ion-content>
    <h1>{{title}}</h1>
  </ion-content>
</ion-view>

thingDetails.js

angular.module('controllers')
  .controller('thingDetails', function($scope, $stateParams) {
    $scope.title = "thing";
});

I've just found the answer to my own problem. I had copied the state from another place in my app and defining the templateUrl and controller in the views object was causing it to play up. I took templateUrl and controller out of the views object and it worked.

Code in state is now as below and works:

.state('sweepstakeDetails', {
  url: '/sweepstakeDetails',
  templateUrl: 'templates/sweepstakeDetails.html',
  controller: 'sweepstakeDetails'
})

If anyone can add a more detailed answer as to why then I'd appreciate it.

Do you have an ui-view called 'menuContent' where the view should be loaded?

Else you could try to use this state without the menuContent views

.state('thingDetails', {
    url: '/thingDetails',
    templateUrl: 'templates/thingDetails.html',
    controller: 'thingDetails'  
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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