简体   繁体   English

角度表单验证,“保存”按钮始终处于禁用状态

[英]Angular Form Validation, Save button always disabled

I have an angular form where the questions are specified in an array. 我有一个角度形式,其中问题在数组中指定。

The HTML is as follows: HTML如下:

<form ng-controller="SupplierController" name="newSupplierForm">

  <p>Enter details for the new Supplier. The new supplier will be added to category {{selectedCategory.description}}</p>

  <p ng-repeat="field in formfields">
     <label class="field" for="{{field.name}}" ng-hide="newSupplierForm.{{field.name}}.$dirty && (newSupplierForm.{{field.name}}.$error.required || newSupplierForm.{{field.name}}.$invalid)">{{field.caption}}</label>
     <label class="error" for="{{field.name}}" ng-show="newSupplierForm.{{field.name}}.$dirty && (newSupplierForm.{{field.name}}.$error.required  || newSupplierForm.{{field.name}}.$invalid)">{{field.caption}}</label>
     <input type="{{field.type}}" name="{{field.name}}" ng-model="newSupplier[field.name]" ng-required="{{field.required}}">    
  </p>
  <button ng-disabled="newSupplierForm.$invalid" ng-click="saveSupplier()">Save</button>

</form>

The Controller is: 控制器为:

financeApp.controller('SupplierController', function($scope, $http) {

$scope.formfields = [
    {caption:'Name :', name:'name', required:"true", type:"text"},
    {caption:'Address :', name:'address1', required:"true", type:"text"},
    {caption:' :', name:'address2', required:"true", type:"text"},
    {caption:' :', name:'address3', required:"", type:"text"},
    {caption:' :', name:'address4', required:"", type:"text"},
    {caption:'Post Code :', name:'postcode', required:"", type:"text"},
    {caption:'Email :', name:'email', required:"", type:"email"},
    {caption:'Phone :', name:'phone', required:"", type:"text"}
];

$scope.newSupplier = {};

$scope.saveSupplier = function() {
    $http.post('/finance/supplier', newSupplier).success(function(data) {
        alert(data);
    });     
}

});

It all appears to work correctly, except that the Save button is never enabled, even if the form is valid. 看起来一切正常,除了即使表单有效,也从未启用过“保存”按钮。 The research I've done so far would indicate that this should work, but it does not. 到目前为止,我所做的研究表明这应该可行,但事实并非如此。

Example : Disable a button if at least one input field is empty using ngDisabled 示例: 如果至少一个输入字段为空,则使用ngDisabled禁用按钮

What am I doing wrong? 我究竟做错了什么?

Also, is there any way of improving this code generally? 另外,是否有任何方法可以改善此代码? This is my first attempt at Angular. 这是我第一次尝试Angular。

Try ng-required="field.required" , without the curly braces, as this already expects an angular expression. 尝试ng-required="field.required" ,不要使用花括号,因为这已经需要一个角度表达式。

And inside the controller, treat the required as true or false, not as a string. 并在控制器内部,将required视为true或false,而不是字符串。


As for how to improve this, the approach you used is ok, but if your form will always have the same fields (it's not dynamic), you should create html elements for each field, and not declare them inside the controller. 至于如何改善这一点,可以使用您使用的方法,但是,如果表单始终具有相同的字段(不是动态的),则应为每个字段创建html元素,而不是在控制器内部声明它们。

The controller should control the behaviour of the view, not what fields to display (in a non dynamic point of view). 控制器应控制视图的行为 ,而不是要显示的字段(在非动态视图中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM