Angularjs 验证表单时如何显示范围?

Angularjs 验证表单时如何显示范围?,angularjs,angularjs-directive,Angularjs,Angularjs Directive,在我的Angular应用程序中,我需要在提交表单时显示一个错误。 使用以下代码,标记永远不会显示 知道我做错了什么吗 <form name="addLocationForm" ng-submit="submitForm(addLocationForm.$valid)" novalidate> <label for="name">Name</label> <input id="name"

在我的Angular应用程序中,我需要在提交表单时显示一个错误。 使用以下代码,标记永远不会显示

知道我做错了什么吗

    <form name="addLocationForm" ng-submit="submitForm(addLocationForm.$valid)" novalidate>
        <label for="name">Name</label>
        <input id="name"
               type="text"
               ng-model="locationName"
               ng-required="true"
               ng-minlength="4"
               ng-maxlength="128">
        <span class="text-error" data-ng-show="addLocationForm.submitted && addLocationForm.name.$invalid">Name is required</span>
        <button type="submit">Add</button>
    </form>

我将改变以下说法:

  <span class="text-error" data-ng-show="submitted && addLocationForm.name.$invalid">Name is required</span>

<button type="submit" data-ng-click="submitted=true">Add</button>
或者您可以尝试类似的方法:

   <form name="loginForm" novalidate 
     ng-app="LoginApp" ng-controller="LoginController" ng-submit="login()">
     <div class="form-group">
       <input class="form-control" name="username" type="text" 
         placeholder="Username" required ng-model="session.username" />
       <span class="help-block" 
         ng-show="loginForm.username.$error.required">Required</span>
     </div>
     <div class="form-group">
       <input class="form-control" name="password" type="password" 
         placeholder="Password" required ng-model="session.password" />
       <span class="help-block" 
         ng-show="loginForm.password.$error.required">Required</span>
     </div>
     <div class="form-group">
         <button type="submit" class="btn btn-primary pull-right" 
           value="Login" title="Login" ng-disabled="!loginForm.$valid">
           <span>Login</span>
         </button>
     </div>
   </form>
使用ng show,您可以添加require、minlength、maxlength等

此链接还可以帮助您:

这解决了我的问题

        <h2>Insert a Location</h2>
        <form name="addLocationForm" ng-submit="submitForm(addLocationForm.$valid)" novalidate>
            <label>Name</label>
            <input type="text"
                   name="name"
                   ng-model="location.name"
                   ng-minlength=3
                   ng-maxlength=20
                   ng-required="true" />
            <div class="error-container" ng-show="addLocationForm.name.$dirty && addLocationForm.name.$invalid">
                <span class="error" ng-show="addLocationForm.name.$error.required">Name is required</span>
                <span class="error" ng-show="addLocationForm.name.$error.minlength">Name has to be at least 4 characters long</span>
                <span class="error" ng-show="addLocationForm.name.$error.maxlength">Name cannot be longer than 128 characters</span>
            </div>
            <button type="submit">Add</button>
        </form>



// Submit the form after all validation has occurred            
$scope.submitForm = function (isValid) {
    // Is form valid?
    if (isValid) {
        // Create a location
        var location = {
            Id: null,
            Name: $scope.location.name
        };
        locationsService.addLocation(location).then(
                function () {
                    alert('Saved');
                    _getLocations();
                },
                function (data) {
                    // Handle error
                    alert(data.data.modelState.location[0]);
                }
            );
    }
};

输入字段需要在作用域上发布“name”属性:确定请删除addLocationForm。$valid from ng submit=submitFormyou可以添加数据ng disabled=!loginForm.$改为对提交按钮有效,但不要在ng submit上使用它,您会遇到什么错误?我想您也忘记了在输入中添加name=login,您添加了id,但您需要name,也请为它可以帮助的模型使用相同的名称
        <h2>Insert a Location</h2>
        <form name="addLocationForm" ng-submit="submitForm(addLocationForm.$valid)" novalidate>
            <label>Name</label>
            <input type="text"
                   name="name"
                   ng-model="location.name"
                   ng-minlength=3
                   ng-maxlength=20
                   ng-required="true" />
            <div class="error-container" ng-show="addLocationForm.name.$dirty && addLocationForm.name.$invalid">
                <span class="error" ng-show="addLocationForm.name.$error.required">Name is required</span>
                <span class="error" ng-show="addLocationForm.name.$error.minlength">Name has to be at least 4 characters long</span>
                <span class="error" ng-show="addLocationForm.name.$error.maxlength">Name cannot be longer than 128 characters</span>
            </div>
            <button type="submit">Add</button>
        </form>



// Submit the form after all validation has occurred            
$scope.submitForm = function (isValid) {
    // Is form valid?
    if (isValid) {
        // Create a location
        var location = {
            Id: null,
            Name: $scope.location.name
        };
        locationsService.addLocation(location).then(
                function () {
                    alert('Saved');
                    _getLocations();
                },
                function (data) {
                    // Handle error
                    alert(data.data.modelState.location[0]);
                }
            );
    }
};