0

I have 2 directives, one is my-form, one is my-field.
Everything works well except I can not get the ngModelController of the input fields.
So I can not get the $dirty, $valid properties of these fields.
For example, when submitting, I want to get the ngModelController of the input with name "field1", but I can not get it.
form.field1 is undefined.
In the FormController "form1", there is no field, any one can help on this?
Many thanks

the code in fiddle is as below:
https://jsfiddle.net/luneyq/0td5hLur/2/

main codes are also listed below:

        angular.module('myApp', [])
                .controller('MyController', ['$scope', function ($scope) {
                    $scope.config = {
                        name: 'form1',
                        fields: [
                            {type: 'text', name: 'field1', model: 'obj.field1'},
                            {type: 'text', name: 'field2', model: 'obj.field2'}
                        ]
                    };
                }])
                .directive('myForm', function() {
                    return {
                        restrict: 'E',
                        replace: true,
                        template: '' +
                            '<form name="{{config.name}}">' +
                            '   <my-field ng-repeat="item in config.fields" config="item"></my-field>' +
                            '    <button ng-click="submit()">submit</button>' +
                            '</form>',
                        scope: {
                            config: '='
                        },
                        link: function(scope, el, attr) {
                            scope.obj = {};
                            scope.submit = function() {
                                var form = scope[scope.config.name];
                                alert(form.field1);
                                alert(form.field1.$dirty);        // error here
                            }
                        }
                    }
                })
                .directive('myField', ['$compile', function($compile) {
                    return {
                        restrict: 'E',
                        replace: true,
                        link: function(scope, iElement, iAttrs) {
                            scope.config = scope.$eval(iAttrs.config);
                            var config = scope.config;
                            var html = '<input type="' + config.type + '" ng-model="' + config.model + '" name="' + config.name + '" />';
                            iElement.after($compile(html)(scope));
                            iElement.remove();
                        }
                    }
                }])
        ;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyController">
    <my-form config="config"></my-form>
</div>

1
  • anyone can help on this??? Commented May 3, 2016 at 2:50

2 Answers 2

1

Try this:

    angular.module('myApp', [])
            .controller('MyController', ['$scope', function ($scope) {
                $scope.config = {
                    name: 'form1',
                    fields: [
                        {type: 'text', name: 'field1', model: ''},
                        {type: 'text', name: 'field2', model: ''}
                    ]
                };
            }])
            .directive('myForm', function() {
                return {
                    restrict: 'E',
                    replace: true,
                    template: '' +
                        '<form name="{{config.name}}">' +
                        '   <my-field ng-repeat="item in config.fields" field="item"></my-field>' +
                        '    <button ng-click="submit()">submit</button>' +
                        '</form>',
                    scope: {
                        config: '='
                    },
                    link: function(scope, el, attr) {
                        scope.submit = function() {
                                var form = scope[scope.config.name];
                                angular.forEach(scope.config.fields, function(item){
                                    console.log(item.name + ": " + form[item.name].$dirty);
                            });                                
                        }
                    }
                }
            })
            .directive('myField', function() {
                return {
                    restrict: 'E',
                    replace: true,
                    template: '' +
                        '<input type="{{field.type}}" ng-model="field.model" name=" {{field.name}}" />',
                    scope: {
                        field: '='
                    }                        
                }
            })
    ;
Sign up to request clarification or add additional context in comments.

2 Comments

yes, your code works, but could you explain why my code failed? Actually, my code is only a simplified version of real one, I need to use the mode of parsing string, because there are kinds of different types of fields in real world.
meanwhile, in your code, the ng-model of every field is the same.
0

I tried it out myself, only modified several lines but it was really not a easy job.
Please kindly refer to another post:

angularjs custom form and field directive: ngModelController is not found in FormControllerenter link description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.