简体   繁体   中英

How to call variables from one javascript to another javascript?

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?

You have to define your variable in the object of service ie 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);
        }
        }
    ]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM