在我的ParentController
我现在有:
$scope.grid = {
$index: null,
view: [],
data: []
};
在我的ChildController
我现在有:
.controller('ChildController', ['$Scope',
function ($Scope) {
$Scope.grid.$index = null;
$Scope.grid.view = [];
$Scope.grid.data = [];
据我了解,这使我可以在中填充$ Scope.grid.data ChildController
,还具有可以在内的$ Scope.grid.data上进行操作的函数Parent Controller
。
现在,我想开始使用“作为控制器”语法。因此,如果我正确理解了这一点,则需要在两个HTML页面中编写代码,例如HTML中的控制器:
<div data-ng-controller="ParentController as parent" ..
<div data-ng-controller="ChildController as child" ..
而我的父控制器是这样的:
this.grid = {
$index: null,
view: [],
data: []
};
如果执行此操作,那么在我ChildController
应该如何引用grid.view中呢?我是否以某种方式将' parent
'传递给ChildController
,然后在我的孩子中使用:
parent.grid.view = [];
最后,我听说“作为控制器”语法是实验性的。现在已经过去了吗?
只要您不更改代码,“ as”语法就不会有任何区别。
如果您更改ParentController
为
this.grid = {
$index: null,
view: [],
data: []
};
那么您仍然需要在中提供$scope
引用ChildController
,但您需要添加另一个属性:
.controller('ChildController', ['$Scope',
function ($Scope) {
$Scope.parent.grid.$index = null;
$Scope.parent.grid.view = [];
$Scope.parent.grid.data = [];
正如你可以看到它不仅是更复杂,但也把代码耦合到HTML:如果你决定你的HTML改变ParentController as someController
你的ChildController
休息。
因此,即使使用“ as”语法,您也希望grid
成为的属性$scope
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句