Angularjs - 控制器继承VS继承范围 [英] Angularjs - Controller inheritance Vs Scope inheritance
问题描述
下面是一个使用范围的继承code,
Below is the code using scope inheritance,
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> Controller inheritance</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script type="text/javascript">
var sample = angular.module("sample", []);
function ParentController(){
this.Name = "Jag";
}
sample.controller("emp", ParentController);
function ChildController(){
this.Sal = 3500;
this.Dept = "Sales";
}
sample.controller("empDetails", ChildController);
</script>
</head>s
<body ng-app="sample">
<div ng-controller="emp as o1">
Employee details of <strong>{{o1.Name}}</strong>:
<div ng-controller="empDetails as o2">
<strong>{{o1.Name}}</strong> earns {{o2.Sal}} and works in {{o2.Dept}} department.
</div>
</div>
</body>
</html>
,其中控制器实例的嵌套的作用域 O2
有指名称
为 o1.Name
。
下面是code代表控制器继承,
Below is the code for controller inheritance,
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> Scope inheritance</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script type="text/javascript">
var sample = angular.module("sample", []);
function ParentController(){
this.Name = "Jag";
}
sample.controller("emp", ParentController);
function ChildController(){
ParentController.call(this);
this.Sal = 3500;
this.Dept = "Sales";
}
ChildController.prototype = Object.create(ParentController.prototype);
sample.controller("empDetails", ChildController);
</script>
</head>
<body ng-app="sample">
<div ng-controller="emp as o1">
Employee details of <strong>{{o1.Name}}</strong>:
</div>
<div ng-controller="empDetails as o2">
<strong>{{o2.Name}}</strong> earns {{o2.Sal}} and works in {{o2.Dept}} department.
</div>
</body>
</html>
其中的 O2
不是嵌套的范围。
where scope of o2
is not nested.
要设计继承层次,哪种方法不AngularJS建议?
To design inheritance hierarchy, Which approach does AngularJS recommend ?
注:术语的控制器继承的不是一个标准术语
Note: Terminology Controller inheritance is not a standard term.
推荐答案
我不知道AngularJS建议是什么,但这里是我的想法。
I wouldn't know what AngularJS recommends, but here are my thoughts.
首先,我从来没有考虑控制器继承之前。但现在,我已经看到了我第一眼发现一些潜在的滋扰。
First of all, I've never considered controller inheritance before. But now that I've seen it I can spot some potential nuisances at first sight.
-
在此情况下,很
ParentController
没有依赖这样的事情很好地发挥出来。但是考虑到以后你需要有一定的依赖性(服务)添加到它,你必须要记住索要该服务在ChildController
,然后将它传递给ParentController
手工制作。这是一个很大的烦恼,如果你问我。
In this very case
ParentController
doesn't have dependencies so things play out nicely. But consider that later you needed to add a certain dependency (a service) to it, you'll have to remember to ask for that service inChildController
and then pass it toParentController
by hand. This is a big annoyance if you ask me.
如果我们看一下HTML,第一种方式(正常范围继承)使得它明显, ChildController
确实需要 ParentController
。第二种情况使事情变得很难看到,如果我在看HTML我看到这两个控制器之间没有任何关系。
If we look at the html, the first way (normal scope inheritance) makes it obvious that ChildController
indeed needs ParentController
. The second case makes things harder to see, if I'm looking at the html I see no relation whatsoever between both controllers.
所以对我来说,范围继承(实际上是做事的默认方式)击败控制器继承以各种方式。
So for me, scope inheritance (which is actually the default way of doing things) beats controller inheritance in every way.
我相信AngularJS球队本身并没有考虑控制器继承(但不要抱着我为),所以最好坚持做事情的方式可验证
I believe the AngularJS team themselves haven't considered controller inheritance (but don't hold me for that), so better stick to the verifiable way of doing things.
这篇关于Angularjs - 控制器继承VS继承范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!