Search code examples
javascriptangularjsalasql

How to use alasql in angular js controller?


I am using alasql in my angular js web app.

I have included

script(type='text/javascript',src='http://cdn.jsdelivr.net/alasql/0.2/alasql.min.js')

in my jade.

In my client side, in my module.js, I have included

angular.module('alasql.core', ['ui.router','restangular','alasql']);

And in my controller, I have injected the alasql.

angular.module('alasql.core')
    .controller('alasql.core.controller', ['$rootScope', '$scope', '$window','alasql',

        function($rootScope, $scope, $window,alasql) {

            function _init() {
                console.log("Inside Home Controller");
                var years = [
                    { yearid: 2012 }, { yearid: 2013 },
                    { yearid: 2014 }, { yearid: 2015 },
                    { yearid: 2016 },
                ];

                var res = alasql.queryArray('SELECT * FROM ? AS years ' +
                    'WHERE yearid > ?', [years, 2014]);

                console.log("Array res");
                console.log(res);
            }

            _init();

            $('.ui.dropdown')
                .dropdown();
            $('.ui.accordion')
                .accordion();
        }
    ]);

I am getting the below error

It says module err. And if I remove the 'alasql' injection , there is no error. But alasql.queryArray function remains undefined.

What is the correct procedure to inject alasql to my angular js controller? Please let me know.

Regards, Sabarisri


Solution

  • Please replace

    alasql.queryArray('SELECT * FROM ? AS years ' +
                    'WHERE yearid > ?', [years, 2014]);
    

    with

    alasql('SELECT * FROM ? AS years ' +
                    'WHERE yearid > ?', [years, 2014]);