热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将绑定传递给组件中的transcluded范围-Passingabindingtotranscludedscopeincomponent

InAngularJS1.5,Iwanttopassabindingfromacomponentintothe(multi-slot)transcludedscope

In AngularJS 1.5, I want to pass a binding from a component into the (multi-slot) transcluded scope - for a reference in the template (in either one specific or all of them - no either way is fine).

在AngularJS 1.5中,我想将一个绑定从一个组件传递到(多槽)转换范围 - 用于模板中的引用(在一个特定的或全部的中 - 没有一种方法可以正常)。

This is for creating a generic custom-select list

这用于创建通用的自定义选择列表

// Component
.component('mySelect', { 
   bind: { 
       collection: '<'
   },
   transclude:{
      header: 'mySelectHeader',
      item: 'mySelectItem'
   },
   templateUrl: 'my-select-template',
   controller: function(){
       ..... 
   }
});

...
// Component template


...
// Example usage

   

   
   {{$item.id}}: {{$item.name}}


Is this possible from a .component()? with custom-directives for the transclusion ?

这可能来自.component()吗?使用自定义指令进行翻译?

2 个解决方案

#1


5  

In your parent component my-select keep a variable like "selectedItem"

在您的父组件中,my-select保留一个像“selectedItem”这样的变量

In your child component my-select-item, require your parent component like below

在您的子组件my-select-item中,需要您的父组件,如下所示

require: {
  mySelect: '^mySelect'
}

And in your my-select-item component's controller, to access your parent component

在my-select-item组件的控制器中,访问您的父组件

 $OnInit= () => {
  this.mySelectedItem= this.mySelect.selectedItem; // to use it inside my-select-item component.
 };
 select($item) {
   this.mySelect.selectedItem = $item; // to change the selectedItem value stored in parent component
 }

So that the selected item is now accessible from

现在可以从中访问所选项目

{{selectedItem.id}}: {{selectedItem.name}}

#2


0  

I ran into this problem as well, and building upon salih's answer, I came up with a solution (disclaimer--see bottom: I don't think this is necessarily the best approach to your problem). it involves creating a stubbed out component for use in the mySelect component, as follows:

我也遇到了这个问题,并在salih的答案的基础上,我提出了一个解决方案(免责声明 - 见底:我认为这不一定是解决问题的最佳方法)。它涉及创建一个用于mySelect组件的存根组件,如下所示:

.component('item', {
    require: { mySelect: '^mySelect' },
    bind: { item: '<' }
})

then, tweaking your template:

然后,调整你的模板:


this will mean there's always an item component with the value bound to it. now, you can use it as a require in a custom component:

这意味着总是有一个项目组件,其值绑定到它。现在,您可以在自定义组件中将其用作需求:

.component('myItemComponent', {
    require: {
        itemCtrl: '^item',
    }
    template: '{{$ctrl.item.id}}: {{$ctrl.item.name}}',
    controller: function() {
        var ctrl = this;
        ctrl.$OnInit= function() {
            ctrl.item = ctrl.itemCtrl.item;
        }
    }
});

and to use it:

并使用它:


   

   
       
   

after I implemented this, I actually decided to change my strategy; this might work for you as well. instead of using a transclude, I passed in a formatting function, i.e.:

实施之后,我实际上决定改变策略;这对你也有用。而不是使用transclude,我传入了一个格式化函数,即:

.component('mySelect', { 
    bind: { 
        collection: '<',
        customFormat: '&?'
    },
    transclude:{
        header: 'mySelectHeader'
    },
    templateUrl: 'my-select-template',
    controller: function(){
        var ctrl = this;
        ctrl.format = function(item) {
            if(ctrl.customFormat) {
                return customFormat({item: item});
            } else {
                //default
                return item;
            }
        }
        ..... 
    }
});

then in the template, just use:

然后在模板中,只需使用:


let me know if you have any feedback or questions!

如果您有任何反馈或问题,请与我们联系!


推荐阅读
author-avatar
书友395154
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有