How to get data from form input

Im new to mobile development, especially using Ionic. Please help me

I have this code for my route

.state('auth', {
url: '/auth',
templateUrl: 'templates/login.html',
controller: 'AuthCtrl'

I have this code for my 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>

and for my 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', {});
    });
}

})

I always get this object {mobile_number: undefined, password: undefined} when calling console.log (credentials) I always set values ​​in my forms, but always undefined. Why?

+4
source share
3 answers

Initialize the scope credential model first:

$scope.credentials = {
   'mobile_number' : ''
   'password' : '',
 }

Then bind your inputs to the area properties:

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


<input type="password" placeholder="Password" ng-model="credentials.password">

And use $scope.credentialsthat now has your form data:

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

. , $scope.credentials . ngModel credentials.password credentials.mobile_number.

+2

, plunker, . , ng-pattern ng-minLength. undefined, . ,

<input type="tel" ng-model="credentials.mobile_number" 
  ng-model-options="{allowInvalid:true}"/>

, , .

+1

Source: https://habr.com/ru/post/1658559/


All Articles