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

jac*_*ack 5 html javascript angularjs angularjs-directive angularjs-scope

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

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

问题我面临的是,当我提出我得到的自定义字段NG-模型的价值形态为空或未定义.因为双向绑定不起作用

你会看到,当我们从父将更新指令范围,但是当你将一个自定义字段不会更新这是使用父范围输入字段和双向绑定后,将无法正常工作更新更新范围

这是我的文件

users.html

<custom-field ng-repeat="(key,field) in custom_fields" form="userForm" ng-model="user.custom_field[field.id]" fielddata="field"></custom-field>
Run Code Online (Sandbox Code Playgroud)

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: "<fielddata",
        form: "<"
    }

    return directive;
});
Run Code Online (Sandbox Code Playgroud)

custom_field_directive.html

<div layout="row" layout-xs="column">
    <div flex >
        <!-- Custom input field html [Required Field]  -->
        <md-input-container class="md-block" ng-if="mydata.type == 'input' && mydata.is_required==1">
            <label>{{mydata.field_name}}</label>
            <input name="{{mydata.slug}}" ng-model="ngModel" ng-required="!ngModel"> 
            <div ng-messages="form[''+mydata.slug+''].$error" ng-show="form[''+mydata.slug+''].$touched" role="alert">
                <div ng-message="required">
                    <span>{{mydata.field_name}} is required</span>
                </div>
            </div>
        </md-input-container>
        <!-- Custom input field html  -->
        <md-input-container class="md-block" ng-if="mydata.type == 'input' && mydata.is_required==0">
            <label>{{mydata.field_name}}</label>
            <input name="phone" ng-model="ngModel"> 
        </md-input-container>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

从数据库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;
        });
    }
Run Code Online (Sandbox Code Playgroud)

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

响应

演示

Plunker

Ita*_*mar 2

关于你的回答@jack。

子作用域是由创建的ngIf

您可以使用ngShow ngHideorngWhen来代替并解决范围界定问题。

$parent一般来说,您应该尽可能避免打电话。

每当您遇到$parent解决问题的情况时,很可能是您的范围有问题或设计错误

ngIf您可以在文档中查看范围

这是相关部分:

在此输入图像描述