AngularJS ng-class 在 ng-repeat 表达式中

AngularJS ng-class inside ng-repeat expression

我正在尝试使用以下代码显示动态表单错误验证,但我在 ng-class 表达式中遇到异常。我的错误是什么,我该如何解决?

.html

<form name="myForm" class="form-horizontal" role="form" ng-submit="submitForm()">
    <div ng-repeat="field in entity.fields">
        <ng-form name="form">

            <!-- TEXT FIELDS -->
            <div ng-if="field.type=='text'" class="form-group" ng-class="{ 'has-error' : 
            form.+field.name+.$dirty && form.+field.name+.$error.required,
            'has-success': form.+field.name+.$valid}">
                <label class="col-sm-2 control-label">{{field.label}}</label>
                <div class="col-sm-6">
                    <input type="{{ field.type }}" dynamic-name="field.name" id="{{field.name}}" ng-model="field.data"
                        class="form-control" required />
                    <p class="help-block" ng-show="{{'form.'+field.name+'.$dirty && form.'+field.name+'.$error.required'}}">Required</p>
                </div>
            </div>

        </ng-form>
    </div>
</form>

.控制器

routerApp.controller('DynamicController1', ['$scope', function ($scope) {
    // we would get this from the api
    $scope.entity = {
        name: "Course",
        fields:
            [
                { type: "text", name: "firstname", label: "Name", required: true, data: "" },
                { type: "radio", name: "color_id", label: "Colors", options: [{ id: 1, name: "orange" }, { id: 2, name: "pink" }, { id: 3, name: "gray" }, { id: 4, name: "cyan" }], required: true, data: "" },
                { type: "email", name: "emailUser", label: "Email", required: true, data: "" },
                { type: "text", name: "city", label: "City", required: true, data: "" },
                { type: "password", name: "pass", label: "Password", min: 6, max: 20, required: true, data: "" },
                { type: "select", name: "teacher_id", label: "Teacher", options: [{ name: "Mark" }, { name: "Claire" }, { name: "Daniel" }, { name: "Gary" }], required: true, data: "" },
                { type: "checkbox", name: "car_id", label: "Cars", options: [{ id: 1, name: "bmw" }, { id: 2, name: "audi" }, { id: 3, name: "porche" }, { id: 4, name: "jaguar" }], required: true, data: "" }
            ]
    };

}]);

https://codepen.io/rama-krishna-the-selector/pen/PXOwQp?editors=1010

angular.js:15536 Error: [$parse:syntax] http://errors.angularjs.org/1.7.5/$parse/syntax?p0=%2B&p1=is%20not%20a%20valid%20identifier&p2=119&p3=%7B%20'has-error'%20%3A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20form.firstname.%24dirty%20%26%26%20form.firstname.%24error.required%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'has-success'%3A%20form.%2Bfield.name%2B.%24valid%7D&p4=%2Bfield.name%2B.%24valid%7D

唯一正确的方法应该是

<div ng-repeat="field in entity.fields">    
  <ng-form name="form">
      <div ng-if="field.type=='text'" class="form-group" ng-class="{ 'has-error' : 
            form[field.name].$dirty && form[field.name].$error.required,
            'has-success': form[field.name].$valid}">
                <label class="col-sm-2 control-label">{{field.label}}</label>
                <div class="col-sm-6">
                    <input type="{{ field.type }}" dynamic-name="field.name" id="{{field.name}}" ng-model="field.data"
                        class="form-control" required />  
                    <p class="help-block" ng-show="form[field.name].$dirty && form[field.name].$error.required">Required</p>
                </div>
        </div>
  </ng-form>

codeopen

重点是访问存储在变量中的 form 对象 属性。这只能通过这种方式完成 form[field.name],但是您所做的 - 您试图将对象属性和名称合并到一个字符串中,这种方式行不通