无法访问控制器[angularJS]中的ng-model值

时间:2018-07-24 17:41:36

标签: angularjs

当我尝试访问控制器中的user.firstname值时,会出现以下错误。

TypeError: Cannot read property 'firstname' of undefined

$scope.signUpCustomer = function(){
        console.log("GGGGGGGGGGGGGGGGGGGGGGGGG ", $scope.user.firstname);
  }
<form class="form-horizontal font-hp-simplified signUpUserForm row" role="form" ng-submit="signUpCustomer()" name ="signUpUserForm" ng-show="!userVendor">
              
              <div class="col-md-12">  
                <div class="col-md-6">
                  <div class="form-group">
                      <label for="firstname" class="col-md-12">First Name:</label>
                      <div class="col-md-12">
                          <input ng-model="user.firstname" type="text" class="form-control" name="firstname" required />
                          <span class="text-danger" ng-show="signUpUserForm.firstname.$invalid && signUpUserForm.firstname.$dirty ">First name is required</span> 
                      </div>
                  </div>
                </div>  
    
                <div class="col-md-6">
                  <div class="form-group">
                      <label for="lastname" class="col-md-12">Last Name:</label>
                      <div class="col-md-12">
                          <input ng-model="user.lastname" type="text" class="form-control" name="lastname" required />
                          <span class="text-danger" ng-show="signUpUserForm.lastname.$invalid && signUpUserForm.lastname.$dirty ">Last name is required</span>
                      </div>
                  </div>              
                </div> 
              </div>  

              <div class="col-md-12">
                <div class="col-md-6">
                  <div class="form-group">
                      <label for="email" class="col-md-12">Email:</label>
                      <div class="col-md-12">
                        <input ng-model="user.email" type="email" class="form-control" name="email" required />
                        <span class="text-danger" ng-show="signUpUserForm.email.$error.required && signUpUserForm.email.$dirty ">Email is required</span>
                        <span class="text-danger" ng-show="signUpUserForm.email.$error.email && signUpUserForm.email.$dirty ">Please enter valid email</span>
                      </div>
                  </div>
                </div> 

                <div class="col-md-6">
                  <div class="form-group">
                    <label for="password" class="col-md-12">Password:</label>
                    <div class="col-md-12">
                      <input ng-model="user.password" type="password" class="form-control" name="password" required />
                      <span class="text-danger" ng-show="signUpUserForm.password.$invalid && signUpUserForm.password.$dirty">Password is required</span>
                    </div>
                  </div>              
                </div>
              </div>  
             
              <div class="col-md-12">
                <div class="col-md-6"> 
                  <div class="form-group">
                      <label for="phone" class="col-md-12">Phone:</label>
                      <div class="col-md-12">
                        <input ng-model="user.phone" type="text" class="form-control" name="phone" required />
                        <span class="text-danger" ng-show="signUpUserForm.phone.$invalid && signUpUserForm.phone.$dirty">Phone is required</span>
                      </div>
                  </div>                                    
                </div>  

                <div class="col-md-6">      
                  <div class="form-group">
                    <label for="country" class="col-md-12">Country:</label>
                    <div class="col-md-12">
                      <select name="country" ng-model="user.country" type="text" class="form-control form-bg-white" required>
                        <!-- <option value="" disabled selected>Select Your Country</option> -->
                        <option value="" disabled selected></option>
                      <option ng-repeat="country in countries" value="{{country}}">{{country}}</option>
                      </select>
                      <span class="text-danger" ng-show="signUpUserForm.country.$invalid && signUpUserForm.country.$dirty">Country is required</span>
                    </div>
                  </div>
                </div>
              </div>  

              <div class="col-md-12">
                <div class="col-md-12">
                  <!-- Button -->                                        
                  <div class="form-group">
                    <div class="col-md-12">
                      <div class="icon-user-signup">
                        <div class="input-group-addon">
                          <i class="fa fa-user"></i>
                        </div>
                        <button id="btn-signup"  class="btn btn-primary col-md-12">SIGN UP</button>
                      </div>                    
                    </div> 
                   <!-- <div class="mrg-top-10 col-md-12 text-right">
                      <h4><a ng-click="go('/seller#/signup')" class="ven-link">
                        Become a vendor today
                      </a></h4>                      
                    </div>-->
                  </div>
                </div>
              </div>  

                
<!--                   <div class="col-md-12 control">
                    <div class="signup-border" >
                        Already have an account ?
                      <a ui-sref="loginUser" >
                        Sign In Here
                    </a>
                    </div>
                  </div> -->
                
         
            </form>

1 个答案:

答案 0 :(得分:1)

您需要首先在控制器中初始化$ scope.customer

$scope.user = {};