简体   繁体   English

AngularJS:如何进行交互并具有隔离范围和父范围?

[英]AngularJS : How to transclude and have both isolate scope and parent scope?

I have a pattern wherein many item types are "editable". 我有一种模式,其中许多项目类型是“可编辑的”。 This means that I have lots of templates (one for each editable item type) that expect to have unique fields, but common functions (edit, save, cancel edit, delete, etc.). 这意味着我有很多模板(每种可编辑项类型一个),它们希望具有唯一的字段,但是具有共同的功能(编辑,保存,取消编辑,删除等)。 These common functions lead to lots of repetition on controllers: save , edit , cancel , etc., and very repetitive error-handling. 这些通用功能导致控制器上的大量重复: saveeditcancel等,以及非常重复的错误处理。

One way I looked at of dealing with this was to have each controller "decorate" itself (using a service), but it got messy as well. 我处理此问题的一种方法是让每个控制器“装饰”自己(使用服务),但是它也很混乱。

I prefer a directive, say, 'editable': 我更喜欢一个指令,例如“可编辑”:

<form name="editGroup" editable>
   <div ng-show="editMode">
    <!-- lots of fields, e.g. -->
    <input type="text" ng-model="name"></input>
    <span ng-show="editGroup.name.$error.required">The name is required</span>

    <button type="submit" ng-click="save()">Save</button>
    <button ng-click="cancel">Cancel</button>
   </div>
   <div ng-show="!editMode">
    <!-- lots of text, e.g. -->
    <span>{{name}}</span>

    <button ng-click="edit()">Edit</button>
    <button ng-click="delete()">Delete</button>
   </div>
</form>

The problem is that all of the models come from the controller scope, since they are unique to this template, while the repetitive scope items, like the functions save() cancel() edit() delete() all come from the directive isolate scope. 问题在于,所有模型都来自控制器作用域,因为它们是此模板唯一的,而重复作用域项(如功能save() cancel() edit() delete()都来自指令隔离作用域) 。

I am, well, mixing scopes, and of course I have no way of knowing in advance what items need to be available. 我是混合示波器,当然,我无法事先知道需要哪些项目。 So if I transclude with: 因此,如果我包含以下内容:

  • isolate scope: I lose access to the controller models in the transcluded element, as well as the form for validations 隔离范围:我无法访问已包含元素中的控制器模型以及验证表单
  • controller scope (default): I lose access to the added on functions from the directive, which was the point of the directive in the first place! 控制器作用域(默认):我无法从指令访问附加功能,这首先是指令的要点!

I am doing something wrong here; 我在这里做错了; what is the better (cleaner?) way to do this? 什么是更好(更清洁?)的方法呢?

I managed to figure it out by shying away from ng-transclude and doing my own transclusion in the link function. 通过避开ng-transclude并在链接函数中进行自己的包含,我设法弄清了这一点。

The following is the equivalent of the normal ng-transclude : 以下是正常ng-transclude的等效项:

link: function (scope,element,attrs,ctrlr,transclude) {
   var sc = scope.$parent.$new();
   transclude(sc,function(clone,scope) {
      element.append(clone); // or however else you want to manipulate the DOM
   });
}

By adding the functions directly onto the transclude child scope, I was able to have everything work, without messing with the parent scope, which I really didn't want to do. 通过将功能直接添加到跨子级作用域中,我可以使所有工作正常进行,而不会弄乱父级作用域,而这实际上是我不想做的。

link: function (scope,element,attrs,ctrlr,transclude) {
   var sc = scope.$parent.$new();
   sc.editMode = false;
   sc.save = function() {
   };
   sc.edit = function () {
     sc.editMode = true;
   };
   // etc.
   transclude(sc,function(clone,scope) {
      element.append(clone); // or however else you want to manipulate the DOM
   });
}

Best of both worlds! 两全其美!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM