[英]Angular (1.5) JS Access Variable from Parent Component in Child Component?
I am attempting to abstract a concept of a ticket list building into an angular app using 2 components. 我正在尝试使用2个组件将票证列表构建到一个有角度的应用程序中。
1st component --> ("Smart Component") Uses $http in order to pull the data and put it inside an array called populatedList that is in the parent ("gather") components controller. 第一个组件->(“智能组件”)使用$ http来提取数据并将其放置在父(“收集”)组件控制器中的名为populatedList的数组中。 This part works.
这部分有效。
2nd component --> ("Dumb component") For each key value pair in the populatedList var from the parent component it should create a list item. 第二个组件->(“哑组件”)对于父组件的populatedList变量中的每个键值对,它都应创建一个列表项。
However the onInit() function for the second component is never called even though I registered it to the same module.
但是,即使我将第二个组件的onInit()函数注册到相同的模块,也不会调用它。
The HTML template code as does not instantiate when I view it through the browser.
当我通过浏览器查看HTML模板代码时,它不会实例化。
I am not getting any errors on my console so what am I doing wrong?
我的控制台上没有任何错误,所以我在做什么错?
Why is my second component not working, even though I used "require" to get info from the parent component.
即使我使用“ require”从父组件获取信息,为什么我的第二个组件也不起作用。
I finally got my child component to see the data from the populated "tickets" variable of the pullTicketCtn controller (evinced by screen shots) but when I try to console.log(vm.parentComp.tickets) or console.log(vm.tickets) it gives me an empty array even though I just did console.log(vm.parentComp) and console.log(vm) and SAW through the console the values in the array. 最终,我得到了我的子组件,以从pullTicketCtn控制器的填充“ tickets”变量中查看数据(如屏幕截图所示),但是当我尝试使用console.log(vm.parentComp.tickets)或console.log(vm.tickets )即使我只是通过控制台执行console.log(vm.parentComp)和console.log(vm)和SAW数组中的值,它也会给我一个空数组。
So now console.log(vm.parentComp.tickets) and console.log(vm) both show my displayTicketsCnt and pullTicketsCnt controllers to both have the tickets var, yet I cannot access it in the child or console.log() it. 因此,现在console.log(vm.parentComp.tickets)和console.log(vm)都显示我的displayTicketsCnt和pullTicketsCnt控制器都具有票证var,但是我无法在其子项或console.log()中访问它。 What Am I doing wrong?
我究竟做错了什么?
pullticketCtn has the data/array pullticketCtn有数据/数组
displayticketCtn has the data/array displayticketCtn具有数据/数组
3rd pic shows those 2 empty arrays [] and [] for console.log(vm.parentComp) and console.log(vm); 第三张图片显示console.log(vm.parentComp)和console.log(vm)的两个空数组[]和[];
/*COMPONENTS*/
/*Parent/smart */
var gather = {
controller:pullTicketsCnt,
controllerAs:'pullTicketsCntAs',
bindings: {
tickets: '=',
client_id:'='
}
};
/* Child/dumb component*/
var list = {
transclude : true,
require:{
/* Require the parent component (^ means its an ancestor)*/
parentComp:'^gather'
},
template: [
'<ul class="main_list">',
'<li ng-repeat:"(key, value) in displayTicketsCntAs.tickets">',
'<span>{{key}}</span>',
'</li>',
'</ul>',
].join(''),
controller:displayTicketsCnt,
controllerAs:'displayTicketsCntAs',
};
/*CONTROLLERS */
function pullTicketsCnt($http){
$http ...
this.tickets=temp.slice(0); //Get the array of populated data
...
}
}
function displayTicketsCnt(){
var vm = this;
vm.$onInit = function(){
console.log('LIST-DATA component initialized');
console.log(vm.parentComp);
console.log(vm);
vm.populated= (vm.parentComp.tickets).slice(0);
/*The two lines below print [] the empty arrays */
console.log(vm.parentComp.tickets);
console.log(vm.populated);
}
}
function listCtrl(){
this.tickets=[];
}
/*Declarations */
var app = angular.module('ticket', [])
.controller('listCtrl',listCtrl) /* Assume this is correct*/
.component('gather',gather)
.component('list',list);
/* HTML*/
<div ng-app="ticket" ng-controller="listCtrl as vm" class="ticket_controller">
<gather tickets="vm.tickets">
<list populated="pullTicketsCntAs.tickets"></list>
</gather>
</div>
transclude : true, should be part of gather not list. transclude:true,应该是不列表的一部分。
You can access the parent controller using 您可以使用以下命令访问父控制器
require: { parent: '^^gather' }, 要求:{父母:'^^ gather'},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.