Symon kt2
Symon kt2

Reputation: 27

$state.go is not working in AngularJS?

How to call ui-view from another Controller in AngularJS

This is my sample program. Actually in here I am using nested ui-view. The problem is when I click the submit button initially it works fine and show an alert SampleController

But again i clicked it doesnt got to SampleController why?

I need to go to that controller when i click on submit button

Is it any error on my code.Please check it my stateProvider too.I am a new starter in AngularJS

Plese correct me Thank you...

var app=angular.module('TestApp', ['angular.filter','ui.router']);
app.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/');
    $stateProvider
        .state('category', {
        views : {   
            "body" : {
                url : '/category',
                templateUrl : 'category.html',
                controller : 'TestController'
            }
        }       
    })
    .state('category.subcategory', {
        url : '/subcategory',
        views : {                              
            "subbody@category" : {
                templateUrl : 'sample.html',
                controller : 'SampleController'              
            }
        }
    })
});

app.controller('MainController', MainController);
function MainController($scope,$state) {
    alert("This is MainController") 
    $scope.getCategory=function(){
        $state.go('category');
    }
}

app.controller('TestController', TestController);
function TestController($scope, $state){    
    $scope.getData=function() {
        alert("Call to Sample Controller")
        $state.go('.subcategory');
    }
}

app.controller('SampleController', SampleController);
function SampleController($scope,$state) {
    alert("This is SampleController")
}

This is my sample HTML files index.html

<!DOCTYPE html>
<html ng-app="TestApp">

  <head>
    <link rel="stylesheet">
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular-route.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.8/angular-filter.js"></script>
        <script src="angular-ui-router.js"></script>    
        <script src="script.js"></script>
  </head>
    <body ng-controller="MainController">
        <a href="#" ng-click="getCategory()">Click to Category</a>
        <div ui-view="body"></div>  
    </body>
</html> 

category.html

<div>      
   <input type="button" name="button" id="button" value="Submit" ng-click="getData()" />
   <div ui-view="subbody"></div>
 </div> 

sample.html

 <div>
    Sample Controller
</div>

I need to hit SampleController when i click submit button

Upvotes: 1

Views: 2779

Answers (1)

Nirmal Borkar
Nirmal Borkar

Reputation: 126

Try this one :

app.controller('TestController', TestController);
  function TestController($scope, $state){    
    $scope.getData=function() {
      alert("Call to Sample Controller")
      $state.go('category.subcategory', null, {reload:true});
  }
}

Upvotes: 3

Related Questions