JSFiddle - React, Tailwind, and code Playground

by Fabian Andres

HTML

<div ng-app="myApp">
    <div ng-controller="appCtrl">
        <h3>dynamic form</h3>
    <my-form ng-model="anmeldung"  ></my-form>

    
    
    <h3>simulate Ajax update</h3>
    <a ng-href="#" ng-click="update1()">update 1</a><br />
    <a ng-href="#" ng-click="update2()">update 2</a><br />
            </div>
</div>

CSS

.error{
    color:red;
}

JavaScript

var app = angular.module('myApp', []);

app.controller('appCtrl', function($scope){
    $scope.anmeldung = undefined;    
    
    $scope.update1 = function(){
         $scope.anmeldung = [
             {
                 Value : 1,
                 Label : 'test',
                     Message : null
             },
             {
                  Value : 2,
                 Label : 'test2',
                     Message : null
             }         
         ];
    }    
    // simulateds server validation by updating property Message
    $scope.update2 = function(){
        $scope.anmeldung = [
            {
                Value : 3,
                Label : 'test',
                Message : null
            },
            {
                Value : 4,
                Label : 'test2',
                Message : 'this field is invalid'
            }         
        ];           
    }
    
});



app.directive('myForm', function ($q, $compile, $parse) {
    return {
        restrict: 'E',        
        link: function ($scope, element, attrs, ngModelCtrl) {
            $q.when($parse(attrs.ngModel)($scope)).then(function (data) {
              $scope.$watch('anmeldung', function (newVal, oldval) {
                 if (newVal != oldval) {                                  
                         newForm = angular.element('<form novalidate name="myform" test="' + Date.now() + '" ng-class="{incative : sending == true}"></form>');
                          angular.forEach(newVal, function (field, index) {                             
                              newElement = angular.element('<input type="text"  test="' + Date.now() + '" ng-model="'+attrs.ngModel + '[' + index + '].Value"></input>');    

                              if(field.Message != null){
                                  console.log('message');
                                 container = angular.element('<div class="error">'+field.Message+'</div>');
                                ...