How to connect AngularJS with MySQL

224 views
Skip to first unread message

deividasdevd

unread,
Sep 21, 2015, 8:33:33 AM9/21/15
to AngularJS
Hello, maybe some1 will be able to help me.

I want somehow connect, my database with this script, so I could get usernames from database, and stop writing over and over names to this list ----> var usernames = ['Jim', 'John', 'Jill', 'Jackie'];      


(function(angular) {
  'use strict';
var app = angular.module('form-example1', []);

app.directive('username', function($q, $timeout) {
  return {
    require: 'ngModel',
    link: function(scope, elm, attrs, ctrl) {
    var usernames = ['Jim', 'John', 'Jill', 'Jackie'];      <--- So maybe there is a way to get usernames from my database, than writing names in this line. 

      ctrl.$asyncValidators.username = function(modelValue, viewValue) {

        if (ctrl.$isEmpty(modelValue)) {
          // consider empty model valid
          return $q.when();
        }

        var def = $q.defer();

        $timeout(function() {
          // Mock a delayed response
          if (usernames.indexOf(modelValue) === -1) {
            // The username is available
            def.resolve();
          } else {
            def.reject();
          }

        }, 2000);

        return def.promise;
      };
    }
  };
});
})(window.angular);

András Csányi

unread,
Sep 21, 2015, 9:15:19 AM9/21/15
to ang...@googlegroups.com
Hi,

You need a webservice (implementation is up to you) which connects to the database and gives you the desired result in JSON format.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
--  Csanyi Andras (Sayusi Ando)  -- http://sayusi.hu -- http://facebook.com/andras.csanyi
--  ""Trust in God and keep your gunpowder dry!" - Cromwell

ma...@exports.io

unread,
Sep 22, 2015, 3:59:14 AM9/22/15
to AngularJS, sayus...@sayusi.hu
Best results I've had were with MySQL using PHP + SLIM to provide REST services on a simple backend hosting site (we used Dreamhost, which offers unlimited MySQL and unlimited PHP facilities).  SLIM does pretty good JSON formatting to deliver via REST, though I had to modify some parts to get what I wanted back into AngularJS.

Its a bit old-school these days, because you can do much better development with Node.JS and MongoDB -- known as the MEAN solution: 


...but, these cost more in hosting at Heroku, Amazon, etc.

My experience with MEAN has been to develop 10x faster and more effective than anything we tried with PHP.  The debugging and coherence of having JavaScript on both client and server is far better, and MongoDB as a server is like butter on toast for all your needs.

I expect Angular 2 will open up lots of good projects that you can download and then set up your own system.  For now there are very good MEAN solutions to learn form.

Marco
Reply all
Reply to author
Forward
0 new messages