【问题标题】:Angular JS Component BindingsAngular JS 组件绑定
【发布时间】:2017-05-24 03:35:02
【问题描述】:

我无法使从父组件的 ng-repeat 循环的项目可用于嵌套组件的范围。父组件是一个包含许多项目组件的轮播。轮播由 ng-repeat 填充。我希望能够访问项目控制器(“it”)中的“项目”和轮播控制器(“cr”)的方法。我想我正在以完全错误的方式解决这个问题。感谢是否有人可以指导我。

carousel.component.html

<slick <!--slick attributes--> >
     <div ng-repeat="item in cr.items">
            <item-component></item-component>
     </div>
</slick>

carousel.component.js

class CarouselController{
  constructor(/*stuff*/){
    'ngInject';    

     this.items =[
       {"something":"The thing 1","otherthing":"The other thing 1"},
       {"something":"The thing 2","otherthing":"The other thing 2"},
       {"something":"The thing 3","otherthing":"The other thing 3"}
     ];
  }

  parentFunctionToCall(item){
    console.log('I called a function on the parent!',item)
  }

  /*other stuff*/
}

export const CarouselComponent = {
  templateUrl: './views/app/components/carousel/carousel.component.html',
  controller: CarouselController,
  controllerAs: 'cr',
  bindings: {
  }
}

item.component.html

<div data-something="{{item.something}}">
  Yo,{{item.otherthing}}!
</div>

<a href="#" ng-click="cr.parentFunctionToCall(item)">
   Trying to call a function on the parent component
</a>

item.component.js

class ItemController{
  constructor($scope,/*stuff*/){
    'ngInject';

     //$scope.it.item & $scope.it.cr are both undefined
     console.log(this);

     //I understand this is the incorrect way but it works
     this.$scope.item = this.$scope.$parent.item;
     console.log(this);
  }

  /*other stuff*/
}

export const ItemComponent = {
  templateUrl: './views/app/components/carousel/item.component.html',
  controller: ItemController,
  controllerAs: 'it',
  bindings: {
    "item":"=",//i can see this as undefined $scope in ItemController
    "cr":"=" //i want to access a method on the parent controller
  }
}

这表明发生了什么... https://plnkr.co/edit/UG20EtI4KxnVnTe8zzz4?p=preview

【问题讨论】:

标签: javascript angularjs binding


【解决方案1】:

使用controllerAs 你没有$scope,你需要使用this。尤其是组件。

this.items =[
   {"something":"The thing 1","otherthing","The other thing 1"},
   {"something":"The thing 2","otherthing","The other thing 2"},
   {"something":"The thing 3","otherthing","The other thing 3"}
 ];

https://docs.angularjs.org/guide/component

【讨论】:

  • 谢谢 - 已编辑。仍然没有从父母那里得到东西
  • 您没有父作用域,它们始终是独立作用域。请参阅链接。您可以要求父控制器并注入它。
  • 是的,我理解,但我认为通过绑定,我应该能够使父元素(即带有 ng-repeat 的 div)的范围可用于 ng-repeat 中的item-component。当然,如果不做访问$scope.$parent.item 的骇人听闻的事情(这确实有效,但破坏了孤立范围的想法),这是可能的
  • 参考文档我所说的是“组件树”中的“组件间通信”
  • 当您查看链接中的表格时,您有两个选择,使用bindingsrequire。看来您现在找到了第一种方法,这是更好的方法。
【解决方案2】:

最后简单得令人沮丧。我不知道为什么这在文档中没有更清楚,但它就像在子组件上设置属性一样简单:

<slick <!--slick attributes--> >
  <div ng-repeat="item in cr.items">
        <!--new item="item" attr-->
        <item-component item="item" call-parent="cr.parentFunctionToCall(item)"></item-component>
  </div>
</slick>

然后绑定按预期工作。访问父函数的行为类似。需要添加一些事件名称作为属性(call-parent,但可以是任何名称)。绑定需要添加到子组件中(如@kuhnroyals 评论):

...
bindings: {
  item:"=",
  callParent: '&'
}

以及子组件上的一些交互事件,例如ng-click="it.callParent()"

这里的工作示例:https://plnkr.co/edit/RIOPs6?p=preview

【讨论】:

  • 您可以使用&amp; 绑定以相同的方式绑定函数。 onDeleteonUpdate 只是示例。
猜你喜欢
  • 2017-05-16
  • 2016-10-23
  • 2016-09-10
  • 1970-01-01
  • 2018-04-18
  • 2018-05-25
  • 2020-11-17
  • 1970-01-01
相关资源
最近更新 更多