我如何在控制器AngularJS之间发送数据

时间:2017-05-31 12:27:32

标签: ionic-framework

在我的场合,如何在控制器AngularJS之间发送数据? 我想将扫描的qr代码的结果发送到[]中,然后显示它。 我是AngularJS和JavaScript的初学者,只为我制作这个程序

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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可以使用$ emit和$ broadcast在控制器之间共享数据: 这些事件也可以用来发送数据,下面给出了经典的例子。您可以在应用程序中共享任何控制器的数据。

                <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>