2017-02-17 122 views
0

我的表单验证在点击提交按钮时不起作用。我已经正确添加控制器HTMl.Any铅会appreciated.Thanks提前:)Angular JS表单验证不起作用

<form id="formbody" ng-submit="submit(user)" name="form" novalidate=""> 

    <div class="formtext" > 

    <div class="has-header"> 
     <ion-label class="labelstyle" > Select Service </ion-label> 
     <input ng-click="goToPage()" name="selectsrve" type="text" class="formtext1 inputimg" ng-model="user.firstname" placeholder=" Select from list" required=""> 
     <!-- <img src="img/icon-arrow-gray.png"></img> --> 
     </input> 
     <span ng-show="user.firstname.$dirty && users.firstname.$error.required">Please select the Service</span> 
    </div> 
    <div style="padding-left: 275px;"> 
     <button type="submit">Submit</button> 
    <!-- <div type="button" id="btn" style="color: red;" >Submit</div> --> 
    </div> 
    </div> 
     </form> 


.controller('ExampleController',function($scope,$location,$scope, $stateParams){ 
    $scope.singleSelect=''; 

    $scope.goToPage=function(){ 
     console.log("selectservice"); 
     $location.path("/selectservice"); 
    } 

    $scope.submit = function(){ 
    // Set the 'submitted' flag to true 
    alert("In the submit button"); 
    $scope.submit=function(user){ 
     alert("user"+user.firstName); 
    }; 

    } 

    }) 

回答

0

$scope.submit功能应该是这样的。删除其中的其他$scope.submit函数。为什么你的输入中有ng-click?当你点击你的输入

$scope.submit = function(user){ 

    alert("user"+user.firstName); 
} 
+0

那是因为我必须按一下在I/P,并从那里我有选择的服务导航到另一个页面。如果它是空的,则应显示错误。 –

0

我已经在你的代码

在你的跨度,而不是user.firstname.$dirty && users.firstname.$error.required你应该使用表单名称输入字段名form.$submitted && form.selectsrve.$invalid && form.selectsrve.$error.required

,你也可以做一些改动重定向请在您的ng-submit提交表格中仅提供有效的使用条件ng-submit="form.$valid && submit(form)"

并在您的控制器中使用范围变量获取用户

$scope.submit=function(){ 
     alert("user "+$scope.user.firstname); 
    }; 

请看看这个plunker