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

时间:2018-01-02 06:28:03

标签: javascript html angularjs angularjs-directive angularjs-scope

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

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

问题我面临的是当我提交表单时,我得到自定义字段的值ng-model为null或undefined。因为双向绑定无法正常工作

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

这是我的文件

users.html

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

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;
});

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>

从数据库 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

2 个答案:

答案 0 :(得分:2)

关于你的答案@jack。

子范围由ngIf创建。

您可以使用ngShow ngHidengWhen来解决范围问题。

通常,您应该尽可能多地调用$parent

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

您可以在docs

中查看ngIf范围

这是相关部分:

enter image description here

答案 1 :(得分:1)

解决了它。

它正在创建子范围,只是将 custom_field_directive.html ngModle更改为$ parent.ngModel

<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="$parent.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="$parent.ngModel"> 
        </md-input-container>
    </div>
</div>