search

Home  >  Q&A  >  body text

angular.js - AngularJS form validation

My AngularJs version is v1.4.3, and I encountered a problem when doing form validation.
The effect I want to achieve is: when the form content is empty, the login button is disabled. But now the login button is not disabled.

-- HTML --

<button class="btn btn-block btn-success" disabled="disabled" name="sign" ng-click="vm.submit(sign_form)" ng-disabled="sign_form.$invalid" type="submit">登 录</button>

-- AngularJS --

var app=angular.module("ayApp",[]);
app.controller("ayAppCtrl",['$scope',function($scope){
    var vm=$scope.vm={};
    vm.d=storedb('ay').find();
    vm.submit=function(sign_form){
        sign_form.$setDirty();
        if(sign_form.$valid){
            alert("Success");
        }
    }
}]);
phpcn_u1582phpcn_u15822754 days ago503

reply all(3)I'll reply

  • 滿天的星座

    滿天的星座2017-05-15 16:56:24

    <input type="text" ng-model="test" required/> <button type="submit" ng-disabled="form.$invalid">提交</button>

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-15 16:56:24

    A vm object is bound to the scope, and then you create a vm. What do you want to do?
    Secondly, I don’t think you need to write a controller yourself to implement this function.

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-15 16:56:24

        <form name="categoryAddBox">
          <input type="text" ng-model="preAddCategoryName" required>
          <input type="submit" ng-click="addCategory($event)" value="确定" ng-disabled="!categoryAddBox.$valid">
        </form>
    

    reply
    0
  • Cancelreply