0

I making a login form with AngularJS. After entering user & password and on clicking the Login button I am trying to retrieve those values. But those are printed as undefined.

My code looks like below.

Javascript

File: app.js

    var mdmApp = angular.module('mdmApp', [ 'ngRoute' ]);

    mdmApp.config(function($routeProvider, $httpProvider) {
        $routeProvider.when('/home', {
            templateUrl : 'partials/home.html',
            controller : 'homeController'
        }).when('/login', {
            templateUrl : 'partials/login.html',
            controller : 'loginController'
        });
    }).controller('homeController', function($scope) {

    }).controller('loginController', function($scope) {
        var user = $scope.user;
        var password = $scope.password;

        $scope.login = function() {
            console.log($scope.user + "-" + $scope.password);
        }
    })

HTML

File : partials/login.html

    <link href="../bower_components/bootstrap/dist/css/bootstrap.css" rel="stylesheet">


    <form role="form" ng-submit="login()">

        <div ng-app="mdmApp" ng-controller="loginController">

                <div class="form-group" >
                    <label for="user" class="control-label">User</label> 
                    <input id="user" type="text" ng-bind="user" class="form-control">
                </div>

                <div class="form-group">
                    <label for="password" class="control-label">Password</label> 
                    <input id="password" type="password" ng-bind="password" class="form-control">
                </div>

                <div class="form-group">
                    <input type="submit" value="Login" class="btn btn-primary"> 
                    <input type="reset" value="Reset" class="btn btn-primary">
                </div>
        </div>
    </form>

1 Answer 1

3

You should be using ng-model, not ng-bind:

<input id="user" type="text" ng-model="user" class="form-control">
<input id="password" type="password" ng-model="password" class="form-control">
Sign up to request clarification or add additional context in comments.

1 Comment

It worked after that change. Thanks. After assigning var user = $scope.user; and print the var user it prints undefined... Can't I assign to a var ?

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.