ng-model is undefined in controller

前端 未结 5 969
醉话见心
醉话见心 2020-12-10 03:56

I\'m using ionic and I have the following view:


  

        
相关标签:
5条回答
  • 2020-12-10 04:03

    Please check this code This is worked for me:

    <div class="row row-center">
      <div class="col">
    
        <div id="logo"></div>
    
        <form>
          <div class="list">
            <label class="item item-input">
              <input type="text" placeholder="Membership No" ng-model="data.membershipNo">
            </label>
            <label class="item item-input">
              <input type="password" placeholder="Password" ng-model="data.password">
            </label>
          </div>
    
          <button class="button button-block button-positive button-login" ng-click="login()">
            Login
          </button>
        </form>
    
      </div>
    </div>
    

    and in your-controller:

    app.controller('loginController', ['$scope',
      function($scope) {
        $scope.data={};
      $scope.login = function () {
        console.log("User logged in with membership no: " + $scope.data.membershipNo +
        "\n and password: " + $scope.data.password);
      }
    
    }]);
    
    0 讨论(0)
  • 2020-12-10 04:13

    I had exact the same issue recently and that's probably a solution: https://stackoverflow.com/a/22768720/552936

    Modified quote:

    "If you use ng-model, you have to have a dot in there."
    Make your model point to an object.property and you'll be good to go.

    Controller

    $scope.formData = {};
    $scope.login = function () {
      console.log("User logged in with membership no: " + $scope.formData.membershipNo +
      "\n and password: " + $scope.formData.password);
     }
    

    Template

    <input type="text" placeholder="Membership No" ng-model="formData.membershipNo">
    <input type="password" placeholder="Password" ng-model="formData.password">
    
    0 讨论(0)
  • 2020-12-10 04:15
    $scope.user = {};
    $scope.submitForm = function (isValid) {
       if (($scope.user.name || '').length > 0){ //Code }
    }
    
    0 讨论(0)
  • 2020-12-10 04:17

    Hey Jean,Have a look of it your code is working here

           <ion-view ng-app="app" hide-nav-bar="true" ng-controller="loginController" class="login-view">
          <ion-content class="padding">
    
            <div class="row row-center">
              <div class="col">
    
                <div id="logo"></div>
    
                <form>
                  <div class="list">
                    <label class="item item-input">
                      <input type="text" placeholder="Membership No" ng-model="membershipNo">
                    </label>
                    <label class="item item-input">
                      <input type="password" placeholder="Password" ng-model="password">
                    </label>
                  </div>
    
                  <button class="button button-block button-positive button-login" ng-click="login()">
                    Login
                  </button>
                </form>
    
              </div>
            </div>
    
          </ion-content>
        </ion-view> 
    
    
    var app= angular.module("app",[]);
        app.controller('loginController', ['$scope', 
          function($scope, $localstorage) {
    
          $scope.membershipNo;
          $scope.password;
          $scope.login = function () {
            alert("User logged in with membership no: " + ($scope.membershipNo || '') +
            "\n and password: " + ($scope.password || ''));
          }
    
        }]);
    
    0 讨论(0)
  • 2020-12-10 04:26

    You need to define your $scope variables like this in your controller:

    $scope.membershipNo = '';
    $scope.password = '';
    

    So your controller would look like:

    app.controller('loginController', ['$scope', '$localstorage',
      function($scope, $localstorage) {
    
      $scope.membershipNo = '';
      $scope.password = '';
      $scope.login = function () {
        console.log("User logged in with membership no: " + $scope.membershipNo +
        "\n and password: " + $scope.password);
      }
    
    }]);
    
    0 讨论(0)
提交回复
热议问题