簡體   English   中英

Angular 表單驗證 ng-disabled 不起作用

[英]Angular form validation ng-disabled not working

我正在嘗試在我的表單中使用角度表單驗證來撰寫博客文章,更具體地說是禁用 ng 的表單。 出於某些原因,我無法確定提交按鈕沒有被禁用,除非所有三個輸入字段都有效,否則應該禁用。 謝謝您的幫助。

這是我的博客模板

 <div ng-controller='BlgoCtrl'>
  <div class='container'>
    <h1> Teewinot Blgo</h1>
    <div class="row">
      <div class='col-md-12'>
        <form role='form' name='blgoPost' novalidate>
          <div class='form-group'>
          <label for='blgoTitle'>Title</label>
          <input name='title' type="title" class='form-control'  placeholder='Technologies of the Future' required><br>
          <label for='blgoContent'>Content</label>
          <textarea name='content' rows='8' type="content" class='form-control' placeholder='The technical innovations of the future will be diverse and impactful on the individual......' required></textarea><br>
          <label for='blgoPassCode'>PassCode</label>
          <input name='passcode' type="passcode" class='form-control' placeholder='&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;' required><br>
          <button type="submit" class='btn btn-primary' ng-disabled="blgoPost.$invalid">Submit Post</button>
          </div>
        </form>
      </div>
    </div>

這是我的 index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Teewinot</title>

  <script src="bower_components/angular/angular.js"></script>

  <script src="bower_components/jquery/dist/jquery.js"></script>
  <script src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="bower_components/angular-route/angular-route.js"></script>
  <link href="bower_components/bootstrap/dist/css/bootstrap.css" rel="stylesheet">

</head>

<body ng-app="Teewinot">
<ng-include src="'app/templates/partials/navbar.html'"></ng-include>

<ng-view></ng-view> 
<!-- angular module defintion and reoutes -->
<script src="app/js/app.js"></script>
<script src="app/js/routes.js"></script>
<!-- controllers -->
<script src="app/js/controllers/blog.controller.js"></script>

</body>
</html>

這是我的博客控制器

angular.module('Teewinot').controller('BlgoCtrl', function($scope, $http) {
  'use strict'
});

您在表單的每個字段上都缺少ng-model 請記住,當您在任何表單字段上提到ng-model時, ng-model會在表單名稱對象中創建具有該特定name屬性的額外對象,然后在表單驗證時考慮這些對象,例如$error$valid$invalid等。

由於您的表單nameblgoPost ,當 angular 編譯此頁面時,它會在內部創建blgoPost名稱范圍內的對象。 並且所有具有nameng-model分配給它們的輸入字段都被添加到該blgoPost對象中。 但是,如果您沒有在表單字段中提及ng-model ,那么它將永遠不會被添加到blgoPost表單對象中。

HTML

<form role='form' name='blgoPost' novalidate="">
    <input name="first" />
    <div class='form-group'>
        <label for='blgoTitle'>Title</label>
        <input name='title' type="title" class='form-control' ng-model="test1" placeholder='Technologies of the Future' required="">
        <br>
        <label for='blgoContent'>Content</label>
        <textarea name='content' rows='8' type="content" ng-model="test2" class='form-control' placeholder='The technical innovations of the future will be diverse and impactful on the individual......' required=""></textarea>
        <br>
        <label for='blgoPassCode'>PassCode</label>
        <input name='passcode' type="passcode" ng-model="test3" class='form-control' placeholder='&#8226;&#8226;&#8226' required="" />
        <br/>
        <button type="submit" class='btn btn-primary' ng-disabled="blgoPost.$invalid">Submit Post</button>
    </div>
</form>

工作小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM