最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Determining if a form is valid using angular - Stack Overflow

programmeradmin0浏览0评论

I'm trying to determine if a form is valid or not using angular.

I have a form like this:

<form name="form" novalidate>
    <p>
        <label>Number: </label>
    <input type="number" min="0" max="10" ng-model="test.number" required />
    </p>
    <p>
        <label>Name: </label>
    <input type="text" ng-model="test.name" required />
    </p>
    <button ng-click="sendTest(test)">Submit</button>
</form>

And, on the sendTest function I did:

angular.module('demo', [
      ]).controller('MainCtrl', function($scope){
        $scope.test = {
          name: 'das'
        };

        $scope.sendTest = function(test) {
          console.log(form.$valid);
          console.log(test.$valid);
        }
      });

The problem is that both form.$valid and test.$valid are undefined. I tried to do this following these examples:

/

Here's the plete code for this demo:

I'm trying to determine if a form is valid or not using angular.

I have a form like this:

<form name="form" novalidate>
    <p>
        <label>Number: </label>
    <input type="number" min="0" max="10" ng-model="test.number" required />
    </p>
    <p>
        <label>Name: </label>
    <input type="text" ng-model="test.name" required />
    </p>
    <button ng-click="sendTest(test)">Submit</button>
</form>

And, on the sendTest function I did:

angular.module('demo', [
      ]).controller('MainCtrl', function($scope){
        $scope.test = {
          name: 'das'
        };

        $scope.sendTest = function(test) {
          console.log(form.$valid);
          console.log(test.$valid);
        }
      });

The problem is that both form.$valid and test.$valid are undefined. I tried to do this following these examples:

http://dailyjs./2013/06/06/angularjs-7/ http://www.youtube./watch?v=J82OD76QhPo

Here's the plete code for this demo: http://plnkr.co/edit/l0E62KPJu4Z2r15VNjJq

Share Improve this question asked Sep 10, 2013 at 17:55 thitemplethitemple 6,0595 gold badges46 silver badges69 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

form gets added to the scope, try: console.log($scope.form.$valid)

BTW, it's called form because that's the value you have specified on the form tag's name attribute. You can also add name attributes to the input fields if you from your controller want to know the state of the specific fields.

Example: http://plnkr.co/edit/Ra98yIFYso94flDIqNCD?p=preview

发布评论

评论列表(0)

  1. 暂无评论