You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular结合Django时ng-disabled与ng-submit失效问题求助

Troubleshooting Angular Form Issues: ng-disabled Not Working & ng-submit Failing

Hey there! Let's work through these two Angular form problems together—they're pretty common when you're getting familiar with Angular's form state management, so let's break it down step by step.

First: Fixing ng-submit Not Working

The most likely culprit here is how you're referencing your submit function. Right now you have ng-submit="Register"—but Angular expects you to call the function, not just name it. That means you need to add parentheses: ng-submit="Register()".

Also, double-check these key points:

  • Make sure your Register() function is actually defined in the controller bound to your form (don't forget the ng-controller directive on your container or form element!).
  • If you moved the submit button outside the form, ng-submit won't trigger it—you'll need to either move the button back inside, or use ng-click="Register()" on the external button (and still confirm the function exists in your controller).
  • Ensure your submit button has type="submit" (if it's inside the form)—a plain <button> without a type might default to type="button", which won't trigger form submission at all.

Next: Fixing ng-disabled Not Working

You mentioned trying $pristine, $dirty, and $touched—but these states only work if Angular can properly track your form controls. Here's what to verify:

  1. Every form control needs an ng-model: Angular can't track $pristine/$dirty states if your inputs, selects, etc., don't have ng-model bindings. Even if you're using Django form fields, you need to add ng-model to each control so Angular can sync with its value and state.
  2. Confirm your form object exists: Your form has name="businessform", so in your controller, you should be able to access $scope.businessform. Add a console.log($scope.businessform) in your controller (or use $watch to catch when it loads) to make sure it's not undefined. If it is, your controller isn't properly bound to the form's scope.
  3. Validate your ng-disabled expression: Make sure it's evaluating to the correct boolean value. For example, if you want to disable the button when the form is invalid or hasn't been modified, use:
    ng-disabled="businessform.$invalid || businessform.$pristine"
    
    Test the expression directly in your controller (e.g., console.log($scope.businessform.$invalid)) to confirm it returns what you expect.
  4. Rule out broken custom validation: If you added any custom validators to your form controls, make sure they're correctly using $setValidity—a faulty validator can mess up the form's overall validity state and break ng-disabled.

Example Fixed Code Snippet

Here's how your form might look with these fixes applied:

{% block content %}
<div class="container" ng-controller="BusinessFormController">
  <form name="businessform" method="post" style="margin-top:5%;" ng-submit="Register()">
    {% csrf_token %}
    <!-- Example form control with ng-model and validation -->
    <div class="form-group">
      <label>Business Name</label>
      <input type="text" name="businessName" class="form-control" ng-model="formData.businessName" required>
      <span ng-show="businessform.businessName.$error.required" class="text-danger">Business name is required</span>
    </div>
    <!-- Submit button with working ng-disabled -->
    <button type="submit" class="btn btn-primary" ng-disabled="businessform.$invalid || businessform.$pristine">Register Business</button>
  </form>
</div>
{% endblock %}

And the corresponding controller:

angular.module('yourApp').controller('BusinessFormController', function($scope) {
  // Initialize form data object to bind with ng-model
  $scope.formData = {};

  // Define your submit function with actual logic
  $scope.Register = function() {
    // Add your submission logic here (e.g., $http POST request)
    console.log('Form submitted with data:', $scope.formData);
  };

  // Debug: Check if the form object loads correctly
  $scope.$watch('businessform', function(form) {
    if (form) {
      console.log('Form state loaded successfully:', form);
    }
  });
});

Give these fixes a try and let me know how it goes—if the problem still persists, feel free to share more details about your controller code or any custom validators you're using!

内容的提问来源于stack exchange,提问作者shashank

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:36:55