0

I'm new in AngularJS. I use WebSQL for database and i've some problem to operate my file. I've 2 javascript file named service.js and controller.js.

service.js :

var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);

            db.transaction(function (tx) {
                var sql = ('CREATE TABLE IF NOT EXISTS CART (id unique, name, price)');
                });

            function addToCart(id, name, price){
                 db.transaction(
                         function (tx) {
                             tx.executeSql(
                                 "INSERT INTO CART (id, name, price) VALUES (NULL, ?, ?);",
                                  [data.name, 
                                  data.price, 

                                  ],
                     );
            }

controller.js :

 angular.module('login').controller('Controller',
        ['Service', '$scope','$state', '$webSql', function(Service, $scope,$state,$webSql) {

            var self = this;
            self.addToCart = addToCart;
            self.database = Service.db;


            function database(){
                self.database = db;
                return database;
            }

            function addToCart(id, name, price) {
                alert("Success add to your cart" + id);
            }
            }
        ]);

How i can call my db from Service.js to Controller.js?

Cesar Wahyu
  • 117
  • 1
  • 1
  • 12

1 Answers1

0

You have to define your variable in the object of service i.e something like :

  this.db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);

       this.db.transaction(function (tx) {
            var sql = ('CREATE TABLE IF NOT EXISTS CART (id unique, name, 
          price)');
            });

        function addToCart(id, name, price){
             this.db.transaction(
                     function (tx) {
                         tx.executeSql(
                             "INSERT INTO CART (id, name, price) VALUES 
 (NULL, ?, ?);",
                              [data.name, 
                              data.price, 

                              ],
                 );
        }

Then you have to import your service , whatever is the name of your service , lets say globalService.

angular.module('login').controller('Controller',
    ['globalService', '$scope','$state', '$webSql', function(globalService, $scope,$state,$webSql) {

        var self = this;
        self.addToCart = addToCart;
        self.database = globalService.db;


        function database(){
            self.database = db;
            return database;
        }

        function addToCart(id, name, price) {
            alert("Success add to your cart" + id);
        }
        }
    ]);
vertika
  • 1,284
  • 1
  • 8
  • 22
  • Thanks sir, in my console said _TypeError: Cannot read property 'transaction' of undefined_ – Cesar Wahyu Oct 25 '17 at 09:33
  • was it working for you before ? when you were using var . As there is no change only change is "this" whihc will not affect the functionality . If it was not working before then you have to check the functions in you db by debugging it . – vertika Oct 25 '17 at 09:50