1

I just starting with AngularJS and I have a routing issue with ui-router. Basically I have a partial that displays a button to sign in. The issue is that clicking on that button does not call the corresponding method in the controller.

The module definition:

var myapp = angular.module('myapp', ['ui.state']);

myapp.config(function($stateProvider, $routeProvider) {

  $stateProvider.state('signin', {
        url : "/", // root route
        views : {
            "signinView" : {
                templateUrl : 'signin.html'
            }
        },
        controller: function($scope) {
            $scope.auth = function() { 
        console.log("clicked");
            };
        }
        //controller: 'LoginController'
    }).state('signedin', {
        views : {
            "signinView" : {
                templateUrl : 'partials/signedin.html'
            }
        }
    });

});

The signin.html:

<button class="btn" ng-click="auth()">Sign In</button>

Plunker thats shows the issue.

2
  • Where is the button in your plunker? Commented Jun 29, 2013 at 16:39
  • I fixed the plunker. The button is displayed. Commented Jun 29, 2013 at 16:49

2 Answers 2

4

The controller definition should be inside the view definition.
Your code should look like this.

myapp.config(function($stateProvider, $routeProvider) {

  $stateProvider.state('signin', {
        url : "", // root route
        views : {
            "signinView" : {
                templateUrl : 'signin.html',
        controller: function($scope) {
                $scope.auth = function() { 
        alert("Clicked");
        console.log("clicked");
            };
        }
            }
        }
        //controller: 'LoginController'
    }).state('signedin', {
        views : {
            "signinView" : {
                templateUrl : 'partials/signedin.html'
            }
        }
    });

Here is the Plunker

Sign up to request clarification or add additional context in comments.

Comments

3

I haven't used ui-router but by the quick look at the code I can see that you controller is defined incorrectly. It needs to reside inside the same object literal that defines your template:

...
"signinView" : {
  templateUrl : 'signin.html',
  controller: function($scope) {
    $scope.auth = function() { 
      alert("Clicked");
      console.log("clicked");
    };
  }
}
...

The ui-router docs are clear about it.

WORKING PLUNKER

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.