简体   繁体   中英

Controller is not getting called in angularjs

I am working on angular js app. I am unable to call my controller in my app on click of button submit. Please check below sample code:

index.html

<!DOCTYPE html>
<html lang="en" ng-app="adminsuite">
<head>
    <meta charset ="utf-8">
    <title></title>
</head>
<body>
<script src="bundle.js" charset="utf-8"></script>
<div class="container">
    <div ui-view></div>
</div>


</body>
</html>

login.html

<div class="col-md-6 col-md-offset-3">
<h2>Login</h2>
<form name="form" ng-submit='login()' role="form">
    <div class="form-group" ng-class="{ 'has-error': form.username.$dirty && form.username.$error.required }">
        <label for="username">Username</label>
        <input type="text" name="username" id="username" class="form-control" ng-model="username" required />
        <span ng-show="form.username.$dirty && form.username.$error.required" class="help-block">Username is required</span>
    </div>
    <div class="form-group" ng-class="{ 'has-error': form.password.$dirty && form.password.$error.required }">
        <label for="password">Password</label>
        <input type="password" name="password" id="password" class="form-control" ng-model="password" required />
        <span ng-show="form.password.$dirty && form.password.$error.required" class="help-block">Password is required</span>
    </div>
    <div class="form-actions">
        <button type="button" ng-disabled="form.$invalid" class="btn btn-primary">Login</button>
    </div>
</form>

index.js

window.onpopstate = function (e) { window.history.forward(1); }
require('jquery/dist/jquery.js');
 require('bootstrap/dist/css/bootstrap.css');
 require('./content/common.css');
require('angular');
require('angular-ui-router/release/angular-ui-router.js');
 require('angular-route/angular-route.js');
require('angular-cookies/angular-cookies.js');
angular.module('adminsuite',  ['ui.router','ngCookies']).config(function($stateProvider, $urlRouterProvider) {

$urlRouterProvider.otherwise('/');
$stateProvider
    .state('login', {
        url: '/',
        templateUrl: 'Login/login.html',
        controller: 'loginController'
    })
    // HOME STATES AND NESTED VIEWS ========================================
    .state('dashboard', {
        url: '/dashboard',
        templateUrl: 'views/dashboard.html',
    });

    // ABOUT PAGE AND MULTIPLE NAMED VIEWS =================================

});
require('./Login/loginController.js');

require('./services/loginAuthenticationService.js');
require('./services/UserServices.js');

loginController.js

angular
    .module('adminsuite')
    .controller('loginController', ['$location', '$rootScope', 'AuthenticationService', '$scope', '$state', function($location, AuthenticationService, $scope, $state) {
    $scope.login = function(){
        $scope.dataLoading = true;
        AuthenticationService.Login($scope.username, $scope.password, function (response) {
            if (response.success) {
                AuthenticationService.SetCredentials($scope.username, $scope.password);
                console.log('Login successful');
                //$rootScope.state = true;
                $state.go('dashboard');
            } else {
                console.log(response.message);
                $scope.dataLoading = false;
                //$rootScope.state = false;
            }
        });
    };
 }

]);

I am not getting any error in console section of browser or in command prompt.

编辑login.html的登录按钮,使其具有type="submit"而不是type="button"

<button type="submit" ng-disabled="form.$invalid" class="btn btn-primary">Login</button>

刚刚从控制器中删除了$ rootScope注入器,因为它没有作为参数传递给控制器​​功能。

you did not assign your controller in the html part try this:

<!DOCTYPE html>
<html lang="en" ng-app="adminsuite">
<head>
    <meta charset ="utf-8">
    <title></title>
</head>
<body ng-controller="loginController">
<script src="bundle.js" charset="utf-8"></script>
<div class="container">
    <div ui-view></div>
</div>


</body>
</html>

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