如何从表单输入中获取数据

mar*_*ght 5 angularjs ionic-framework

我是移动开发的新手,尤其是使用Ionic.请帮帮我

我的路线有这个代码

.state('auth', {
url: '/auth',
templateUrl: 'templates/login.html',
controller: 'AuthCtrl'
Run Code Online (Sandbox Code Playgroud)

我有这个代码用于我的login.html

<ion-view view-title="Login" name="login-view">
<ion-content class="padding">
    <div class="list list-inset">
        <label class="item item-input">
            <input type="text" placeholder="Mobile Number" ng-model="mobile_number">
        </label>
        <label class="item item-input">
            <input type="password" placeholder="Password" ng-model="password">
        </label>
    </div>
    <button class="button button-block button-calm" ng-click="login()">Login</button>
</ion-content>
Run Code Online (Sandbox Code Playgroud)

并为我的AuthCtrl

.controller('AuthCtrl', function($scope,$auth,$state) {

$scope.login = function() {
    var credentials = {
        mobile_number : $scope.mobile_number,
        password : $scope.password,
    }
    console.log(credentials);
    $auth.login(credentials).then(function(data) {
        // If login is successful, redirect to the users state
        $state.go('tab.dash', {});
    });
}
Run Code Online (Sandbox Code Playgroud)

})

我总是在调用console.log(凭证)时得到这个对象{mobile_number:undefined,password:undefined} 我总是把值放在我的表单中,但它总是未定义的.为什么?

Aki*_*fas 7

首先初始化范围凭证模型:

$scope.credentials = {
   'mobile_number' : ''
   'password' : '',
 }
Run Code Online (Sandbox Code Playgroud)

然后将输入绑定到范围属性:

<input type="text" placeholder="Mobile Number" ng-model="credentials.mobile_number">


<input type="password" placeholder="Password" ng-model="credentials.password">
Run Code Online (Sandbox Code Playgroud)

并利用$scope.credentials它现在有你的表格数据:

$auth.login($scope.credentials).then(function(data) {
    // If login is successful, redirect to the users state
    $state.go('tab.dash', {});
});
Run Code Online (Sandbox Code Playgroud)