Search code examples
javascriptnode.jsangularjsstates

$state.go method not switching to other state - angularjs


In my controller, I have $state.go('purchase');, but am having the following error :

Error: Could not resolve'purchase'from state ''.

But initially, I defined url '' to '/home' in my file states.js, which goes to state 'home'. Why is my state transition not happening? Please let me know where I am wrong.

states.js

/**
 * Dashboard routes.
 */
define(['./templates'], function(templates) {
    var mod = angular.module("item.states", ["ui.router"]);
    console.log("Inside item home states");

    var StateProvider = function($stateProvider, $urlRouterProvider) {
         $urlRouterProvider.when('', '/home');
        $stateProvider            
            .state("item", {
                url: '/home',
                template: templates.home,
                controller : 'controller.item.home.main'
            })
            .state("purchase", {
                url: '/purchase',
                template: templates.purchase,
                controller : 'controller.item.home.main'
            });
    }
    StateProvider.$inject = ["$stateProvider", "$urlRouterProvider"];

    mod.config(StateProvider);
    return mod;
});

controller.js

function _purchase(_event){
console.log("Clicked purchase button");
$state.go('purchase');
}

main.js

define(['./controllers','./states'], function(controllers,states) {
  var mod = angular.module("item.home", ['common.services.ItemService',
     'common.filters.format']);
  mod.controller('controller.item.home.main', controllers.main);
  console.log("Inside item home main");
  return mod;
});

my server side jade

extends ../layouts/default
block content
    div(ng-controller="controller.item.home.main as main" ng-init="item= #{JSON.stringify(item)}")
        .masterhead.segment
            .ui.page.grid.stackable
                .row.ui.basic.segment
                    .four.wide.column.ui.center.aligned
                        img.ui.image.rounded(src="/images/itemicon.png")
                        h2() !{item.name} - !{item.activeseason.name}
                    .twelve.wide.column
                        .ui.small.orange.button(id="purchaseBtn" ng-click="purchase($event)”)
                                | Purchase
        div.ui.page.grid(ui-view)

Thanks.


Solution

  • The error was because, In my main.js - I missed to include the module 'item.states'

    define(['./controllers','./states'], function(controllers,states) {
      var mod = angular.module("item.home", ['common.services.ItemService',
         'common.filters.format','item.states']);
      mod.controller('controller.item.home.main', controllers.main);
      console.log("Inside item home main");
      return mod;
    });