[英]How do I refer to the scope of a parent inside a child controller when using the “as controller” syntax?
In my ParentController
I currently have: 在我的
ParentController
我目前有:
$scope.grid = {
$index: null,
view: [],
data: []
};
In my ChildController
I currently have: 在我的
ChildController
我目前有:
.controller('ChildController', ['$Scope',
function ($Scope) {
$Scope.grid.$index = null;
$Scope.grid.view = [];
$Scope.grid.data = [];
From what I understand this allows me to populate $Scope.grid.data in the ChildController
and also have functions that can operate on the $Scope.grid.data inside the Parent Controller
. 据我了解,这使我可以在
ChildController
填充$ Scope.grid.data,并且还具有可以在Parent Controller
的$ Scope.grid.data上进行操作的函数。
Now I would like to start using the "as controller" syntax. 现在,我想开始使用“作为控制器”语法。 So if I understand this correctly I need to code inside the two HTML pages the controllers in my HTML like this:
因此,如果我正确理解了这一点,则需要在两个HTML页面中编写代码,例如HTML中的控制器:
<div data-ng-controller="ParentController as parent" ..
<div data-ng-controller="ChildController as child" ..
And my parent controller like this: 而我的父控制器是这样的:
this.grid = {
$index: null,
view: [],
data: []
};
If I do this then in my ChildController
how should I refer to the grid.view? 如果执行此操作,那么在
ChildController
应如何引用grid.view? Do I somehow pass ' parent
' into the ChildController
and then in my child use: 我是否以某种方式将“
parent
”传递给ChildController
,然后在我的孩子中使用:
parent.grid.view = [];
Finally I heard that the "as controller" syntax was experimental. 最后,我听说“作为控制器”语法是实验性的。 Has it now got past that stage ?
现在已经过去了吗?
As long as you don't change your code the "as" syntax doesn't make any difference. 只要您不更改代码,“ as”语法就不会有任何区别。
If you changed ParentController
to 如果您将
ParentController
更改为
this.grid = {
$index: null,
view: [],
data: []
};
then you would still need a $scope
reference in your ChildController
, but you add another property: 那么您仍然需要在
ChildController
使用$scope
引用,但要添加另一个属性:
.controller('ChildController', ['$Scope',
function ($Scope) {
$Scope.parent.grid.$index = null;
$Scope.parent.grid.view = [];
$Scope.parent.grid.data = [];
As you can see it's not only more complicated, but you also couple the code to the HTML: If you decide to change your HTML to ParentController as someController
your ChildController
breaks. 正如你可以看到它的不仅更加复杂,但你也把代码耦合到HTML:如果你决定改变你的HTML
ParentController as someController
您ChildController
休息。
So even with the "as" syntax you want grid
to be a property of $scope
. 因此,即使使用“ as”语法,您也希望
grid
成为$scope
的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.