简体   繁体   English

Angular.js最佳实践 - 扩展控制器,覆盖控制器默认值

[英]Angular.js best practice - extending controllers, overriding controller defaults

Here is a real-life Angular problem I can't wrap my head around. 这是一个现实生活中的角度问题,我无法解决这个问题。 I love Angular, but this issue is bugging me a lot right now. 我喜欢Angular,但这个问题现在让我烦恼不已。

What is the best practice to extend an existing controllers's functions, and use the extended controller on an other page of my application? 扩展现有控制器功能的最佳实践是什么,并在我的应用程序的其他页面上使用扩展控制器? In other words: How to do controller inheritance in Angular? 换句话说: 如何在Angular中进行控制器继承?

Edited out - 23/09/2014, dont think the description of my original usecase helps the visitors to understand it better what I'm after here. 编辑出来 - 2014年9月23日,不要认为我原来的用例的描述可以帮助访问者更好地理解我在这里的意思。 I think it disctracts people from the real issue. 我认为它将人们从真正的问题中解脱出来。

After half a year I think I understand completely what's going on. 半年后,我想我完全理解发生了什么。 As it is pointed out in a comment to this post, the simplest answer is services. 正如在这篇文章的评论中指出的那样,最简单的答案是服务。

In the most optimal case, all your scope variables are values gathered from a factory/service. 在最佳情况下,所有范围变量都是从工厂/服务收集的值。 Still, you might want to use the exact same controller with one extra function: $scope.someFunction(){}, and keep the rest. 不过,您可能希望使用完全相同的控制器和一个额外的函数:$ scope.someFunction(){},并保留其余部分。 In this case, you do have a 'thin' controller logic, which is the desirable controller design - but may still end up a hundred or more lines of code. 在这种情况下,你确实有一个“瘦”控制器逻辑,这是理想的控制器设计 - 但仍然可能最终导致一百或更多行代码。 You don't want that being duplicated in an other controller, just because you need some extra controller logic (like $scope.someFunction() ) 您不希望在其他控制器中复制,只是因为您需要一些额外的控制器逻辑(如$ scope.someFunction())

What do you do then? 那你怎么办呢?

The answer is this: 答案是这样的:

  1. Make sure that you did everything in order to solve the situation with factories 确保你做了一切,以解决工厂的情况
  2. if you are abolutely certain you did, go for the controller injection: 如果你绝对肯定你做了,那就去控制器注射:

     .controller('childController', function ($scope, $controller) { 'use strict'; $controller('parentController', {$scope: $scope}); $scope.someFunction=function(){} }) 

It's that simple. 就这么简单。 -- again, usually, things can be solved with factories.. - 通常情况下,工厂可以解决问题..

Hope you find this useful ;) 希望你觉得这个有用 ;)

Abstract 抽象

The problem you are referring to, looks to me like generic code reuse problem for which there is standard solution in the field of computer studies (invented back in previous century), ie inheritance . 您所指的问题在我看来就像通用代码重用问题,在计算机研究领域(在上个世纪发明)中存在标准解决方案,即继承 Unfortunately due to numerous reasons OOP didn't obtain wide adoption in JavaScript community, one of them is the fact that it wasn't core feature of the language. 不幸的是,由于众多原因,OOP没有在JavaScript社区中得到广泛采用,其中之一就是它不是该语言的核心功能。 Another one is the fact that developers are open to creating their own approaches at solving the same problem. 另一个原因是开发人员愿意在解决同样的问题时创建自己的方法。 And by creating these different approaches developers partition the community into parts that deal with inheritance in different ways. 通过创建这些不同的方法,开发人员将社区划分为以不同方式处理继承的部分。

It leads to blurring of whole idea of OOP and makes people scared of those three letters. 它导致整个OOP概念模糊,让人们害怕这三个字母。 In my practice I've met dozen's of people saying that JavaScript isn't made for OOP and raising points like "JavaScript it's a functional language", or "inheritance is an anti-pattern" or "Don't do things for which JavaScript wasn't intended for". 在我的实践中,我遇到了十几个人,说JavaScript不是为OOP而制作的,而是提出像“JavaScript它是一种功能语言”,或“继承是一种反模式”或“不要为JavaScript做事”不是为了“。 But when you ask these people what OOP is?... Typically none of them can provide an answer. 但是当你问这些人OOP是什么时候?......通常他们都不能提供答案。 So having an opinion is great but it has to be attached to some investigation. 所以有一个意见是很好的,但它必须附加到一些调查。

Answer 回答

So answering your question, I would propose using John Resig's JavaScript inheritance approach, which described in detail within the following SO post: angularjs with oop inheritance in action 所以回答你的问题,我建议使用John Resig的JavaScript继承方法,该方法在以下SO帖子中有详细描述: angularjs with oop inheritance in action

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

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