0

How i can send data between controllers AngularJS in my occasion? I wanna send result of scanned qr code into things[] and,of course, show it. I'm beginner in AngularJS and JavaScript and making this program just for me

App.js:

var MKscanner = angular.module('starter', ['ionic', 'ngCordova', 'ngStorage'])

MKscanner.controller('scanBarCtrl', function($scope, $cordovaBarcodeScanner) {
  
$scope.input ={
  MyText : ''
};


  $scope.scanBarcode = function() {
    $cordovaBarcodeScanner.scan(
      {
        preferFrontCamera : false,
        orientation : 'portrait'
      }).then(function (result) {
        alert(result.text);
              },
      function (error) {
        alert('Scanning failed: ' + error);
      });
  };

});

MKscanner.factory ('StorageService', function ($localStorage) {

  $localStorage = $localStorage.$default({
    things: []
  });

  var _getAll = function () {
    return $localStorage.things;
  };

  var _add = function (thing) {
    $localStorage.things.push(thing);
  }

  var _remove = function (thing) {
    $localStorage.things.splice($localStorage.things.indexOf(thing), 1);
  }

  return {
    getAll: _getAll,
    add: _add,
    remove: _remove
  };
});


MKscanner.controller( 'MainCtrl', function ($scope, StorageService) {
  $scope.things = StorageService.getAll();

  $scope.add = function (newThing) {
    StorageService.add(newThing);
      };

  $scope.remove = function (thing) {
    StorageService.remove(thing);
  };
});
<div ng-controller="MainCtrl">
            <div class="list">
                <div class="item item-input-inset">
                    <label class="item-input-wrapper">
                        <input type="text" placeholder="Save your own text" ng-model="newThing">
                    </label>
                    <button class="button button-clear button-positive icon" ng-click="add(newThing)">
                        <i class="ion-ios-plus-outline"></i> Add
                    </button>
                </div>
            </div>

            <ion-list show-delete="false" can-swipe="true">
                <ion-item ng-repeat="thing in things">
                    {{thing}}
                    <ion-option-button class="button-assertive ion-trash-b" ng-click="remove(thing)"></ion-option-button>
                </ion-item>
            </ion-list>
            </div>
Maksym
  • 1
  • 1
  • 2
    Possible duplicate of [How to transfer the data between controllers](https://stackoverflow.com/questions/43902680/how-to-transfer-the-data-between-controllers) – Sangwin Gawande Jun 05 '17 at 12:57

1 Answers1

0

You can share data between controllers using $emit and $broadcast : These events can be used to send data as well, The classic example is given below. You can share data any controller in the app.

                <div ng-app="myApp" ng-controller="myCtrl"> 
                <button ng-click="sendData();"></button>
            </div>


            <script>
                var app = angular.module('myApp', []);
                app.controller('myCtrl', function($scope, $http) {
                    function sendData($scope) {
                        var arrayData = [1,2,3];
                        $scope.$emit('someEvent', arrayData);
                    }

                });
                app.controller('yourCtrl', function($scope, $http) {
                    $scope.$on('someEvent', function(event, data) {
                        console.log(data); 
                    }); 
                });
            </script>
Sangwin Gawande
  • 7,658
  • 8
  • 48
  • 66