我正在尝试为表单中的自定义字段创建一个指令.我能够创建指令但不能更新范围.
有关问题的更多信息,请参阅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;
});
}
这是我提交表单时的用户模型
演示
Plunker
最佳答案
关于你的答案@jack.
子范围由ngIf创建.
您可以使用ngShow ngHide或ngWhen代替并解决范围问题.
通常,您应该尽可能避免调用$parent.
每当遇到$parent修复问题的情况时,很可能是您的示波器问题或设计错误
您可以在docs中查看ngIf范围
这是相关部分:
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!