Angularjs ng已禁用myForm。$无效,无法工作

Angularjs ng已禁用myForm。$无效,无法工作,angularjs,angular-material,Angularjs,Angular Material,如果用户没有填写所有必填字段,我尝试了不同的方法来禁用我的表单提交按钮 我试图了解这是如何工作的,因为它是随机发生的,我总是使用类似的方法 以下是html: <form name="newContactForm" novalidate> <md-card layout="column" layout-align="center" layout-wrap> <md-card-content flex="50"> <div layout=

如果用户没有填写所有必填字段,我尝试了不同的方法来禁用我的表单提交按钮

我试图了解这是如何工作的,因为它是随机发生的,我总是使用类似的方法

以下是html:

<form name="newContactForm" novalidate>
  <md-card layout="column" layout-align="center" layout-wrap>
   <md-card-content flex="50">
     <div layout="row" layout-align="center" layout-wrap>
       <md-input-container flex="25">
         <label>Name</label>
         <input name="name" ng-model="newContact.name" required>
       </md-input-container>
       <md-input-container flex="25">
         <label>Telephone</label>
         <input name="tel" ng-model="newContact.tel" required>
       </md-input-container>
       <md-input-container flex="25">
         <label>Email</label>
         <input name="email" ng-model="newContact.email" required>
       </md-input-container>
       <md-input-container flex="25">
         <label>Title</label>
         <input name="title" ng-model="newContact.title" required>
       </md-input-container>
       <md-input-container flex="100">
         <label>Notes</label>
         <textarea name="notes" ng-model="newContact.notes" required md-maxlength="150" maxlength="150"></textarea>
       </md-input-container>
     </div>
   </md-card-content>
   <md-card-actions layout="row" layout-align="end center">
     <md-button ng-disabled="newContactForm.$invalid" ng-click="create(newContact)">Submit</md-button>
     <md-button>Cancel</md-button>
   </md-card-actions>
  </md-card>
</form>

它返回
无法读取未定义的属性“$valid”

尝试说
!有效的
而不是无效的,如中所示

提交


请让我知道这是否有效

试着说
!有效的
而不是无效的,如中所示

提交


请让我知道这是否有效

您能尝试使用
按钮
而不是
md按钮
吗。或者请确保表单的所有字段都应该有name属性。将此
{{newContactForm}}
放到html中,然后您可以跟踪表单对象中的更改,您将能够找到原因。你的代码应该没问题。它工作得很好。如果不是wokring,请提供一个具体的案例。hi@nextt1我已经编辑了这个问题。我尝试了
console.log($scope.newContactForm.$valid)但返回为undefined@SimonSch不打印任何东西的üpbach?o、 o请尝试使用
按钮
代替
md按钮
。或者请确保表单的所有字段都应该有name属性。将此
{{newContactForm}}
放到html中,然后您可以跟踪表单对象中的更改,您将能够找到原因。你的代码应该没问题。它工作得很好。如果不是wokring,请提供一个具体的案例。hi@nextt1我已经编辑了这个问题。我尝试了
console.log($scope.newContactForm.$valid)但返回为undefined@SimonSch不打印任何东西的üpbach?o、 气味现在只会使其禁用,即使在填充fields@Johhan:注意
$valid
而不是
$invalid
@SimonSchüpbach我尝试了它,运气不好,当我尝试为
newContactForm.name添加
ng消息时,$error
也不起作用,这就像它没有读取字段或其他东西。现在这样做只会使它在填充fields@Johhan:注意
$valid
而不是
$invalid
@SimonSchüpbach我尝试了它,运气不好,当我尝试为
newContactForm.name添加
ng消息时,$error
也不起作用,好像它没有读字段什么的。
setTimeout(function () {
  console.log($scope.newContactForm.$valid);
}, 2000);