首页 > 解决方案 > angularjs用组件将祖父母传给孙子

问题描述

如果可以的话,让我举一个例子:

<grandparent>
    <parent>
        <grandson>

如果祖父母有一个对象,让我们说一个人

const person = {
    name: 'William',
    age: 102
}

孙子如何能够从祖父母那里继承这种价值?

<title={{ $ctrl.person.name }}>

不会工作,因为这$ctrl将是父母。

希望我的轻松例子也能如此。

标签: angularjsangularjs-components

解决方案


检查这个 plunkr

这是更新的代码:

<grand-parent>
  <cbs-cus-comp com-bind='grandParentCntAs.name'>
    <child child-com-bind='cbsCusCompCntAs.name'></child>
  </cbs-cus-comp>
</grand-parent>

您需要提供以下内容才能实现这一目标:

  var cbsCusComp        =   {
                            transclude  :   true,
                            require: {grandParentComp:'^grandParent'},
                            template    :   'Parent : <b>{{cbsCusCompCntAs.comBind}}</b><br /><ng-transclude></ng-transclude>',
                            controller  :   cbsCusCompCnt,
                            controllerAs:   'cbsCusCompCntAs',
                            bindings    :   {comBind:'='}
                        };

推荐阅读