I have a simple controller, and would like to introduce a very similar additional controller, 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'd like to create a controller that is 90% equal to the code of the parentController. The main differences:
- get
getQueryString()should return a different query - the response properties setter to the
$scopeshould contain 1 changed line - I need additional
filterfunctions for inside the new controller, that should only exist for it, but not for theparentController.
Coming from java, I'd solve this for example with inheritance and overridden methods, like:
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 I achieve similar with angularjs?