Angular.js best practice - expanding controllers, overriding default settings for controller

Here is the real Angular problem that I cannot wrap my head in. I love Angular, but this problem annoys me a lot.

What is the best practice for expanding existing controller functions and using the advanced controller on another page of my application? In other words: How to make controller inheritance in Angular?

Edited - 09/23/2014, I don’t think that the description of my original usecase helps visitors better understand what I am doing here. I think this distracts people from the real problem.

+23
javascript angularjs angularjs-controller
Jan 31 '14 at 15:21
source share
2 answers

Six months later, I think I understand what is happening. As noted in the commentary on this post, the simplest answer is services.

In the most optimal case, all scope variables are values ​​collected from factory / service. However, you can use the same controller with one additional function: $ scope.someFunction () {} and save the rest. In this case, you have a “thin” controller logic, which is the desired controller design, but may still contain one hundred or more lines of code. You do not want this duplication in another controller, simply because you need additional controller logic (e.g. $ scope.someFunction ())

What do you do?

The answer is:

  • Make sure that you have done everything to solve the situation with the plants.
  • If you are absolutely sure you did, go to the controller input:

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

It is so simple. - again, as a rule, things can be solved with factories.

I hope you find this useful;)

+43
Feb 17 '14 at 11:47
source share

annotation

The problem you are talking about looks to me like a problem of reusing common code, for which there is a standard solution in the field of computer research (invented in the previous century), i.e. inheritance . Unfortunately, for many reasons, OOP is not widespread in the JavaScript community, one of which is the fact that it is not the main language feature. Another is the fact that developers are open to creating their own approaches to solving the same problem. And by creating these different approaches, developers break the community down into parts that deal with inheritance in different ways.

This erodes the whole idea of ​​OOP and makes people afraid of these three letters. In my practice, I met a dozen people saying that JavaScript is not designed for OOP and raises points such as “JavaScript is a functional language”, or “inheritance is an anti-pattern” or “Do not do what JavaScript was not intended for " But when you ask these people what is OOP? ... Usually none of them can give an answer. Therefore, opinion is of great importance, but it must be applied to any study.

Answer

Thus, in answer to your question, I would suggest using the JavaScript JavaScript inheritance approach in JavaScript, which is described in detail in the following SO post: angularjs with oop inheritance in action

-one
Nov 17 '15 at 9:10
source share



All Articles