Using database in AngularJS - where should I write DB connection code? Using database in AngularJS - where should I write DB connection code? angularjs angularjs

Using database in AngularJS - where should I write DB connection code?


I believe the best practice is to have an http route that you can get the list from. Let's assume it's a list of articles, then you:

  1. have a route in your web server from which you could : GET /articles (you could easily implement it using mongodb driver collection & find functions)
  2. on your angular controller :

(client code)

function searchCtrl($scope, $http){    $http.get("/articles").success(function(articles, status, headers, config) {          $scope.articles = articles    }}

as for your second question, you could render the list from the server, but if you want to update the list, you will need to use $http regardless. moreover, note that angular templates use {{}}, so you might override them if you're not careful - that's why I think it's not a good practice.if, however, you had some configuration you want to inject from your web server, then you could inject a code similar to this (as a script):

angular.module("myModule.configuration", []).constant('myConfiguration', {siteName:"http://www.my-site.com");

and then you could inject 'myConfiguration' to all your controllers (don't forget to add "myModule.configuration" to the dependencies array)