У меня возникли проблемы с созданием элемента из ng-repeat из родительского компонента, доступного для области вложенного компонента. Родительский компонент - это карусель, который содержит много элементов. Карусель заполнен ng-repeat. Я хочу иметь доступ к «элементу» и методу контроллера карусели («cr») в контроллере элемента («он»). Наверное, я собираюсь сделать это совершенно неправильно. Цените, если кто-нибудь может дать мне руль.Угловые JS Компонентные привязки
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
Необходимо обновить кода в рабочий фрагмент, и я буду рад взглянуть. –
@PhilPoore поступающий, высоко оцененный –
@PhilPoore, если ваше предложение все еще стоит - https://plnkr.co/edit/UG20EtI4KxnVnTe8zzz4?p=preview –