1

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.com/2013/06/06/angularjs-7/ http://www.youtube.com/watch?v=J82OD76QhPo

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

4

1 に答える 1

7

formスコープに追加されます。試してください:console.log($scope.form.$valid)

ところで、これformは form タグの name 属性で指定した値であるため、呼び出されます。コントローラーから特定のフィールドの状態を知りたい場合は、名前属性を入力フィールドに追加することもできます。

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

于 2013-09-10T18:25:31.837 に答える