angularjs自定义表单和字段指令:在FormController中找不到ngModelController

月亮

我有2个指令,一个是我的形式,一个是我的领域。
我需要为这两个指令使用动态创建html内容的模式。

一切正常,除非我无法获得输入字段的ngModelController。
因此,我无法获取这些字段的$ dirty,$ valid属性。例如,在提交时,我想获取名称为“ field1”的输入的ngModelController,但我无法获取它。
form.field1未定义。
在FormController“ form1”中,没有字段,任何人都可以帮忙吗?
非常感谢

小提琴中的代码是:https : //jsfiddle.net/0td5hLur/3/

主要代码也列在下面:

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', ['$compile', function($compile) {
                    return {
                        restrict: 'E',
                        replace: true,
                        scope: {
                            config: '='
                        },
                        compile: function(element, attrs, transclude) {
                            return {
                                pre: function (scope, iElement, iAttrs, controller) {
                                    console.log('-------myForm');
                                    var html = '<form name="{{config.name}}">' +
                                            '   <my-field ng-repeat="item in config.fields" config="item"></my-field>' +
                                            '    <button ng-click="submit()">submit</button>' +
                                            '</form>';
                                    iElement.append($compile(html)(scope));
                                    scope.obj = {
                                        field1: '1',
                                        field2: '2'
                                    };
                                    scope.submit = function () {
                                        var form = scope[scope.config.name];
                                        console.log(form);
                                        alert(form.field1);
                                        alert(form.field1.$dirty);        // error here
                                    }
                                }
                            };
                        }
                    }
                    }])
                        .directive('myField', ['$compile', function($compile) {
                            return {
                                restrict: 'E',
                                replace: true,
                                scope: {
                                    config: '='
                                },
                                compile: function(tElement, tAttrs, transclude) {
                                    return {
                                        pre: function(scope, iElement, iAttrs, controller) {
                                            var config = scope.config;
                                            var html = '<input type="' + config.type + '" ng-model="' + config.model + '" name="' + config.name + '" />';
                                            iElement.after($compile(html)(scope.$parent));
                                            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>

月亮

我自己尝试了一下,只修改了几行,但这确实不是一件容易的事。
最终的工作代码如下:

我错过了以下行:
iElement.after(html);
var element = iElement.next();
iElement.remove();
$ compile(element)(scope。$ parent);

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', ['$compile', function($compile) {
  return {
    restrict: 'E',
    replace: true,
    scope: {
      config: '='
    },
    link: function (scope, iElement, iAttrs, controller) {
      console.log('-------myForm');
      var html = '<form name="{{config.name}}">' +
          '   <my-field ng-repeat="item in config.fields" config="item"></my-field>' +
          '    <button ng-click="submit()">submit</button>' +
          '</form>';
      iElement.append($compile(html)(scope));
      scope.obj = {
        field1: '1',
        field2: '2'
      };
      scope.submit = function () {
        var form = scope[scope.config.name];
        console.log(form);
        alert(form.field1);
        alert(form.field1.$dirty);        // error here
      }
    }
  }
}])
.directive('myField', ['$compile', function($compile) {
  return {
    restrict: 'E',
    replace: true,
    scope: {
      config: '='
    },
    link: function(scope, iElement, iAttrs, controller) {
      var config = scope.config;
      var html = '<input type="' + config.type + '" ng-model="' + config.model + '" name="' + config.name + '" />';
      iElement.after(html);
      var element = iElement.next();
      iElement.remove();
      $compile(element)(scope.$parent);
    }
  }
}])
        ;
<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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

angularjs:在FormController中找不到ngModelController

来自分类Dev

在自定义表单请求中找不到路由

来自分类Dev

找不到自定义字段

来自分类Dev

在自定义指令上使用ngModelController更改父指令中的模型

来自分类Dev

AngularJS中的自定义指令和路由

来自分类Dev

用于自定义指令表单控件的AngularJS自定义表单验证器

来自分类Dev

AngularJS自定义指令-匹配多个字段?

来自分类Dev

AngularJS自定义指令

来自分类Dev

从输入的自定义指令中,如何使父表单无效?

来自分类Dev

ANgularjs:ng-repeat和嵌套的自定义指令

来自分类Dev

自定义ngTagsInput和autoComplete指令(AngularJS)

来自分类Dev

AngularJS和自定义方法/ HTML的指令

来自分类Dev

ANgularjs:ng-repeat和嵌套的自定义指令

来自分类Dev

无法从AngularJS + Stormpath的注册表单中添加自定义字段

来自分类Dev

使用自定义指令,ng-show和$ scope在Angularjs中隐藏/显示元素

来自分类Dev

如何在AngularJS中实现自定义格式和输入指令(解决超越范围的问题)?

来自分类Dev

使用自定义指令,ng-show和$ scope在Angularjs中隐藏/显示元素

来自分类Dev

Angular JS指令定义-需要ngModelController以及自定义控制器

来自分类Dev

AngularJS-自定义指令中的点击访问

来自分类Dev

自定义指令模板中的AngularJS控制变量

来自分类Dev

在AngularJS自定义验证指令中调用异步服务

来自分类Dev

使用指令绑定angularjs中的自定义事件

来自分类Dev

AngularJS在自定义指令中包装ui选择

来自分类Dev

在自定义指令中绑定数据-AngularJS

来自分类Dev

在Angularjs自定义指令中获取属性的值

来自分类Dev

AngularJS指令中自定义HTML标记的后果

来自分类Dev

AngularJS-在自定义指令中更新变量

来自分类Dev

在Angularjs自定义指令中获取属性的值

来自分类Dev

用于表单验证的自定义指令

Related 相关文章

  1. 1

    angularjs:在FormController中找不到ngModelController

  2. 2

    在自定义表单请求中找不到路由

  3. 3

    找不到自定义字段

  4. 4

    在自定义指令上使用ngModelController更改父指令中的模型

  5. 5

    AngularJS中的自定义指令和路由

  6. 6

    用于自定义指令表单控件的AngularJS自定义表单验证器

  7. 7

    AngularJS自定义指令-匹配多个字段?

  8. 8

    AngularJS自定义指令

  9. 9

    从输入的自定义指令中,如何使父表单无效?

  10. 10

    ANgularjs:ng-repeat和嵌套的自定义指令

  11. 11

    自定义ngTagsInput和autoComplete指令(AngularJS)

  12. 12

    AngularJS和自定义方法/ HTML的指令

  13. 13

    ANgularjs:ng-repeat和嵌套的自定义指令

  14. 14

    无法从AngularJS + Stormpath的注册表单中添加自定义字段

  15. 15

    使用自定义指令,ng-show和$ scope在Angularjs中隐藏/显示元素

  16. 16

    如何在AngularJS中实现自定义格式和输入指令(解决超越范围的问题)?

  17. 17

    使用自定义指令,ng-show和$ scope在Angularjs中隐藏/显示元素

  18. 18

    Angular JS指令定义-需要ngModelController以及自定义控制器

  19. 19

    AngularJS-自定义指令中的点击访问

  20. 20

    自定义指令模板中的AngularJS控制变量

  21. 21

    在AngularJS自定义验证指令中调用异步服务

  22. 22

    使用指令绑定angularjs中的自定义事件

  23. 23

    AngularJS在自定义指令中包装ui选择

  24. 24

    在自定义指令中绑定数据-AngularJS

  25. 25

    在Angularjs自定义指令中获取属性的值

  26. 26

    AngularJS指令中自定义HTML标记的后果

  27. 27

    AngularJS-在自定义指令中更新变量

  28. 28

    在Angularjs自定义指令中获取属性的值

  29. 29

    用于表单验证的自定义指令

热门标签

归档