In my Angular application, where should I keep a list of countries (long static array) and how to reference it from contoller? I don't want to store it in controller.
Just create constant service
and put all your countries list
in that provider.
And just inject
that service into your controller
where you need that values.
app.constant("country", [
{"code": "AFG","name":"Afghanistan"},
{"code":"ALB","name":"Albania"},
{"code":"DZA", "name":"Algeria"},
{"code":"ASM", "name":"American Samoa"},
..........]); //countries List
Inject this service into controller.
app.controller("demoCtrl",['$scope','country',function($scope,country){
//countries automatically imported into controller
}]);
Angular Doc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With