简体   繁体   English

AngularJS中的继承和嵌套视图

[英]Inheritance and nested views in AngularJS

I'm trying to set up some nested views in angularjs. 我正在尝试在angularjs中设置一些嵌套视图。 I've been using the ui-router library to do this which works great for the most part. 我一直在使用ui-router库来执行此操作,这在大多数情况下效果很好。 The problem is that there is a separate controller for each view with no real inheritance going on between them. 问题在于,每个视图都有一个单独的控制器,它们之间没有真正的继承。 If I want to modify something in a parent controller from a child controller I have to use $scope.$parent . 如果要从子控制器修改父控制器中的某些内容,则必须使用$ scope。$ parent。 This is a bit of a pain and it can become worse if there are multiple levels of inheritance and you have to remember which level the variable you are accessing is on. 这有点麻烦,如果有多个继承级别,并且必须记住要访问的变量位于哪个级别,则情况可能会变得更糟。 Also if you forget to use $parent in your child controller and you try to modify one of the parent's variables, Angular will create a new instance of the variable which could lead to some hard to track down bugs. 另外,如果忘记在子控制器中使用$ parent并尝试修改父变量中的一个,Angular将创建该变量的新实例,这可能会导致某些难以跟踪的错误。

Ideally I would just be able to use prototype inheritance. 理想情况下,我将能够使用原型继承。 This would also map nicely into classes in Typescript or Coffeescript. 这也可以很好地映射到Typescript或Coffeescript中的类中。 One way I thought of to do this would be to get rid of all the parent controllers and just have the child controllers which would inherit any common functionality from prototypes (super classes). 我想到的一种方法是摆脱所有父控制器,只拥有子控制器,这些子控制器将从原型(超类)继承任何常用功能。 Then you would just have to throw the controller up on the $rootScope so that the parent views could access it. 然后,您只需要将控制器放在$ rootScope上,以便父视图可以访问它。

Can anyone think of any issues with this solution or better solutions? 谁能想到这个解决方案或更好的解决方案有任何问题? Would I be better off just using $parent and letting Angular handel the "inheritance". 仅使用$ parent并让Angular处理“继承性”会更好吗?

Thanks 谢谢

You should be able to enable protypal inheritance if you modify the source slightly. 如果您稍微修改源,则应该能够启用原型继承。

Open the file ./ui-router/src/viewDirective.js and you should find this near the top: 打开文件./ui-router/src/viewDirective.js,您应该在顶部附近找到它:

var directive = {
    restrict: 'ECA',
    terminal: true,
    transclude: true,
    ...

Add another line such that it reads: 添加另一行,使其显示为:

var directive = {
    restrict: 'ECA',
    terminal: true,
    transclude: true,
    scope: true,
    ...

This should enable inheritance. 这应该启用继承。 I can't find a good direct source on this, but this you tube playlist is full of all sorts of angular goodness. 我找不到很好的直接来源,但是这个您的播放清单充满了各种棱角分明的优点。

As John pointed out the $scope objects inherit from each other but the actual controllers do not. 正如John指出的那样,$ scope对象是彼此继承的,但实际的控制器却不是。 So I decided to set it up so that the controllers do inherit from each other. 因此,我决定对其进行设置,以使控制器之间确实相互继承。 To do this I have one root controller that just looks like this: 为此,我有一个看起来像这样的根控制器:

function rootCtrl($scope) {
    $scope.ctrl={};
    $scope.ctrl.scope = $scope;
}

Then in my most deeply nested child controllers I have something like this 然后在我最深层嵌套的子控制器中,我有这样的东西

function myCtrl($scope, $dependency1, $dependency2) {
    myCtrlImpl.apply($scope.ctrl, [$http, $dependency1, $dependency2]);
}

function myCtrlImpl($dependency1, $dependency2) {
    this.someVariableThatIsAccessableEverywhere = ":)";

    //If I want to access scope from here I can just do something like
    //this.scope.$watch...
}

Now if I want to move any functionality from myCtrlImpl into a base prototype, I can just use standard prototype inheritance. 现在,如果我想将任何功能从myCtrlImpl移至基本原型,则可以使用标准原型继承。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM