加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

javascript – 表单中自定义字段的指令[范围未更新]

发布时间:2020-12-14 22:40:19 所属栏目:资源 来源:网络整理
导读:我正在尝试为表单中的自定义字段创建一个指令.我能够创建指令但不能更新范围. 有关问题的更多信息,请参阅Plunker演示 我面临的问题是当我提交表单时,我得到自定义字段的值ng-model为null或undefined.因为双向绑定不起作用 您将看到我们从父级更新范围时它将

我正在尝试为表单中的自定义字段创建一个指令.我能够创建指令但不能更新范围.

有关问题的更多信息,请参阅Plunker演示

我面临的问题是当我提交表单时,我得到自定义字段的值ng-model为null或undefined.因为双向绑定不起作用

您将看到我们从父级更新范围时它将更新指令范围,但是当您在自定义字段中更新它时,它将不会更新使用父范围的输入字段,之后双向绑定将不起作用

这是我的文件

users.html

app.js

app.directive('customField',function () {
    var directive = {};
    directive.restrict = 'E';
    directive.templateUrl = 'app/modules/custom_fields/views/custom_field_directive.html';
    directive.scope = {
        ngModel: "=",mydata: "

custom_field_directive.html

从数据库UsersController.js获取自定义字段值的函数

$scope.getCustomFields = function (id = "") {
        $rootScope.loader = true;
        $http.post(site_settings.api_url + 'get_custom_fields_admin_user',{id: id})
                .then(function (response) {
                    $scope.custom_fields = response.data;
                    angular.forEach($scope.custom_fields,function (value,key) {
                        if (value.field_values) {
                            $scope.user.custom_field[value.id] = value.field_values.value;
                            console.log("in");
                        } else {
                            $scope.user.custom_field[value.id] = null;
                        }
                    });
                    $rootScope.loader = false;
                }).catch(function (error) {
            $rootScope.message = 'Something Went Wrong.';
            $rootScope.$emit("notification");
            $rootScope.loader = false;
        });
    }

这是我提交表单时的用户模型

Response

演示

Plunker

最佳答案
关于你的答案@jack.

子范围由ngIf创建.

您可以使用ngShow ngHide或ngWhen代替并解决范围问题.

通常,您应该尽可能避免调用$parent.

每当遇到$parent修复问题的情况时,很可能是您的示波器问题或设计错误

您可以在docs中查看ngIf范围

这是相关部分:

enter image description here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读