大家好,我试图用角形创建一个表单,我有以下定义:

{
        "options": {
            "data": {
                "someData": 6
            }
        },
        "fieldGroup": [
            {
                "className": "ctnStepper",
                "key": "customField ",
                "type": "form-section"
            },
            {
                "className": "ctnChamp",
                "key": "inputTest",
                "type": "input"
            }
        ]
}


我想将ng-class属性放在inputTest字段上,以便之后生成的表单将产生如下内容:

<input ng-class="{ 'myClass': model. Something != somethingElse }">


我曾尝试对ngModelElAttrs进行这种改装,但很难理解,因此我被困在这里。预先感谢您的答复。

奥古斯丁

最佳答案

我已将类分配给div,您可以根据需要使用。

ng-class="{'alert-danger':x.key=='customField', 'alert-info':x.key=='inputTest'}"

function myCtrl($scope) {


    $scope.myData={
        "options": {
            "data": {
                "someData": 6
            }
        },
        "fieldGroup": [
            {
                "className": "ctnStepper",
                "key": "customField",
                "type": "form-section"
            },
            {
                "className": "ctnChamp",
                "key": "inputTest",
                "type": "input"
            }
        ]
}
}

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<div ng-app ng-controller="myCtrl">
    <div ng-repeat="x in myData.fieldGroup">
      <div class="alert" ng-class="{'alert-danger':x.key=='customField', 'alert-info':x.key=='inputTest'}">{{x.className}}</div>
    </div>
</div>

关于javascript - 将ng-class属性放到成 Angular 字段中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40421763/

10-09 20:34