Access data from one controller to another controller - AngularJS

Address Book Controller :

$http({
        method: 'GET',
        url: '/api/getnewgroup'
    })
    .then(function (response) {
        $scope.draft.groups = response.data;
        $scope.groups = response.data; // updated
    }, function (response) {
        console.log(response);
    });

In this controller above, I get json response in $ scope.draft.groups , I have this draft object in another controller profsmsController strong>.

profsmsController :

$scope.draft = {
            draftType: '',
            scheduledTime: '',
            senderdata: '',
            draftData: {
                contacts: ''
            },
            groups: {
                select: false
            },
            senderName: '',
            message: '',
            draftName: '',
            createdOn: '',
            updatedOn: ''
        };

How to access the $ scope object?

My controller:

angular
    .module('sampleApp.controllers', [])

    //addressbook page controller
    .controller('addressbookCtrl', function ($http, $scope, $rootScope, $location,
        $state, toastr, $timeout, $window, sharedService) {

        // Groups

        // get group

        $http({
            method: 'GET',
            url: '/api/getnewgroup'
        })
        sharedService.getDraftPromise().then(function (response) {
            $scope.groups = response.data;
            $scope.draft.groups = response.data;
        }, function (response) {
            console.log('error');
        });

})



.controller('profsmsCtrl', function ($http, $scope, $rootScope, $location,
                $state, toastr, $timeout, $window) {



                /* for drafts */

                $scope.draft = {
                    draftType: '',
                    scheduledTime: '',
                    senderdata: '',
                    draftData: {
                        contacts: ''
                    },
                    groups: {
                        select: false
                    },
                    senderName: '',
                    message: '',
                    draftName: '',
                    createdOn: '',
                    updatedOn: ''
                };


                //add draft
                $scope.addmanualInputDraft = function () {
                    $http.post('/api/addmanualinputdraft', $scope.draft).then(function (response) {
                        toastr.success("Added successfully!");
                        $('.bd-example-modal-lg-manual').modal('hide');
                        $state.reload();
                    });
                }
        })

My services.js:

angular
   .module('sampleApp.services', [])
   .factory('sharedService', function ($http) {

      var draftPromise = $http({
         method: 'GET',
         url: '/api/getnewgroup'
      });
      return {
         getDraftPromise: function () {
            return draftPromise;
         }
      };

   });

my app.js:

'use strict';
angular
  .module('sampleApp', ['sampleApp.controllers', 'sampleApp.directives','sampleApp.services','sampleApp.filters','ui.router','toastr','ngSanitize', 'ui.select'])
  .config(function($stateProvider, $urlRouterProvider, $locationProvider) {

    $locationProvider.hashPrefix('');
    $urlRouterProvider.otherwise('/dash');
    $stateProvider
      .state('dash', {
        url: '/dash',
        templateUrl: 'partials/dash',
      })
      .state('quicksms', {
        url: '/quicksms',
        templateUrl: 'partials/quicksms',
        controller: 'quicksmsCtrl'
      })
      .state('professionalsms', {
        url: '/professionalsms',
        templateUrl: 'partials/professionalsms',
        controller: 'profsmsCtrl'
      })
      .state('file2sms', {
        url: '/file2sms',
        templateUrl: 'partials/file2sms',
        controller: 'file2smsCtrl'
      })
      .state('addressbook', {
        url: '/addressbook',
        templateUrl: 'partials/addressbook',
        controller: 'addressbookCtrl'
      })
  });

This is the updated complete code. I want to access the $ scope.draft.groups object from the address book controller.

+4
source share
1 answer

In general, you want to create a service that contains your shared data:

myApp.factory('sharedService', function($http) {

    var draftPromise = $http({
        method: 'GET',
        url: '/api/getnewgroup'
    });

    return {
        getDraftPromise: function() {
            return draftPromise;
        }
    };
});

, :

myApp.controller("myController", function($scope, sharedService) {

    sharedService.getDraftPromise().then(function(response) {
        $scope.draft.groups = response.data;
    });
});

draftPromise.

:, , , . .

+2

Source: https://habr.com/ru/post/1691820/


All Articles