AngularJS - 获取'未定义'

时间:2015-10-14 14:28:29

标签: javascript angularjs undefined

我使用AngularJS制作登录表单。进入用户&密码和单击登录按钮我试图检索这些值。但这些都是未定义的。

我的代码如下所示。

的Javascript

档案: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

文件: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 个答案:

答案 0 :(得分:3)

您应该使用key,而不是ng-model

ng-bind