Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing long static array in Angular

Tags:

angularjs

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.

like image 604
Petr B Avatar asked Jun 14 '14 13:06

Petr B


1 Answers

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

like image 200
chandu Avatar answered Oct 19 '22 15:10

chandu