Angular js magic strings
In angularJs, what's the optimal approach to get rid of magic strings,
For example,
//app.js
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])
//accountController.js
angular.module('starter.controllers')
.controller('AccountCtrl',
function($scope) {
}
);
angular.module('starter')
.config(function config($stateProvider, $urlRouterProvider) {
$stateProvider.state($area.state, {
url: '/account',
views: {
'tab-account': {
templateUrl: 'templates/tab-account.html',
controller: 'AccountCtrl'
}
}
});
I think about changing to
//constants.js
var MODULE_STARTER = function(){return 'starter';};
var MODULE_CONTROLLERS = function(){return 'starter.controllers';};
var MODULE_SERVICES = function(){return 'starter.services';};
//app.js
angular.module(MODULE_STARTER(), ['ionic', MODULE_CONTROLLERS(), MODULE_SERVICES()])
angular.module(MODULE_CONTROLLERS, []);
//accountController.js
var ACCOUNT_CONTROLLER = function() {
var $area = {};
$area.url = '/account';
$area.state = 'tab.account';
$area.controllerName = "AccountCtrl";
$area.stateConfig = function config($stateProvider, $urlRouterProvider) {
$stateProvider.state($area.state, {
url: $area.url,
views: {
'tab-account': {
templateUrl: 'templates/tab-account.html',
controller: $area.controllerName
}
}
});
};
return $area;
};
angular.module(MODULE_STARTER())
.config(ACCOUNT_CONTROLLER().stateConfig);
angular.module(MODULE_CONTROLLERS())
.controller(ACCOUNT_CONTROLLER().controllerName,
function($scope) {
}
);
So the thing is, when I refer to the same string value, I don't have to repeat magic strings, it would also be easier to refactor.
Also if I want to pass an account controller or redirect url, I can always get data from ACCOUNT_CONTROLLER
Does this approach have any disadvantages?
+3
source to share
No one has answered this question yet
Check out similar questions: