I have a simple controller and I want to create a new controller that is very similar to it, but without copying too much code.
angular.module('test').controller('parentController', parentController);
parentController.$inject = ['$scope', '$location', 'someService'];
function myController($scope, $location, someService) {
var params = getQueryString();
var rsp = executeQuery(params);
processResponse(rsp);
function getQueryString() {
return "?param=1&someparam=2";
}
function executeQuery(params) {
...
}
function processResponse(rsp) {
//process rsp, convert some parts, and populate model properties, like
$scope.model.prop1 = rsp.data.prop1;
}
};
Now I am looking to create a new controller with 90% similarity to the parentController
. The main changes needed are:
- The
getQueryString()
function should return a different query - A single line in the response property setter for
$scope
needs to be changed - I also require additional
filter
functions specific to the new controller
Coming from a java
background, I would solve this using inheritance and overridden methods. For example:
public cass ParentClass {
String getQueryString() {
return "?param=1&someparam=2";
}
}
public class CustomClass extends ParentClass {
@Override
String getQueryString() {
return "?customparam=1";
}
@Override
void processResponse(rsp) {
super.processResponse(rsp);
//read "rsp.paramX" additionally
}
}
But how can a similar approach be achieved with angularjs
?