angularjs: 在 FormController 中找不到 ngModelController

angularjs: ngModelController is not found in FormController

我有 2 个指令,一个是我的表单,一个是我的字段。
一切正常,除了我无法获取输入字段的 ngModelController。
所以我无法获取这些字段的$dirty, $valid 属性。
比如提交的时候想获取name为"field1"的input的ngModelController,但是获取不到
form.field1 未定义。
在 FormController "form1" 中,没有字段,任何人都可以帮助解决这个问题?
非常感谢

fiddle中的代码如下:
https://jsfiddle.net/luneyq/0td5hLur/2/

主要代码如下:

        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>

试试这个:

    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: '='
                    }                        
                }
            })
    ;

自己试了一下,只修改了几行,确实不容易。
请参考另一个 post: