2

I have referenced a lot of questions in stack over flow for this topic. But none of them seems to work for me.

I have two html pages: AddUser.html and ListUser.html. Both pages have the same ng-app="myApp" directive, but two different controllers.

AddUser.html
ng-app="myApp" ng-controller="userCtrl"

ListUser.html
ng-app="myApp" ng-controller="listCtrl"

I need to pass the data added from Adduser page to Listuser page. For this i have written a service

app.js

var ang = angular.module('myApp', ['ngRoute']);
ang.config(function ($routeProvider) {
    $routeProvider
        .when('/Add_Users', {
            templateUrl: 'Add_Users.html',
            controller: 'userCtrl'
        })
        .when('/List_Users', {
            templateUrl: 'List_Users.html',
            controller: 'listCtrl'
        })
        .otherwise({
            redirectTo: '/Add_Users'
        });
});



service.js
ang.service('userService', function () {
    var userList = [];

    var addUser = function (newObj) {
        console.log("called addUser");
        userList.push(newObj);
        console.log("userList AFTER ADD::" + userList);
    };

    var getUsers = function () {
        console.log("called getUsers" + userList + " >>");
        return userList;
    };

    return {
        addUser: addUser,
        getUsers: getUsers
    };

});

In controller i am calling the service methods. like below

ang.controller('userCtrl', function ($scope,userService) {
 userService.addUser($scope.user_details);
});

and retrieving it in the next controller

ang.controller( "listCtrl",function($scope,userService){
    $scope.userlist =  userService.getUsers();
});

But in the second page, the object is empty. Tried a number of solutions, but none of them seems to work.

Is this because of the ng-app directive in the second page,causing the service to be re-initialzed?

9
  • Use service for passing data Commented Aug 4, 2015 at 10:36
  • If you read my question, i have written a service. Commented Aug 4, 2015 at 10:37
  • Have you implemented a routing? (inside your angular app) Commented Aug 4, 2015 at 10:37
  • If you are using 2 separate ng-apps, then "yes". The service will be re-initialized. Commented Aug 4, 2015 at 10:38
  • Yes. I have include that in my question as well. Commented Aug 4, 2015 at 10:38

3 Answers 3

1

Is this because of the ng-app directive in the second page,causing the service to be re-initialzed?

You need in fact 3 html pages.

The first one will have to contain all of your scripts, the "ng-app" and a directive ng-view.

In this case, the ng-view will be replaced by your 2 other html pages.

For the moment, when you switch from one page to another, all of your javascript is initialized, so you lose everything you had.

If, you can't have a "root" page, there is still another thing you can try : put some data inside the session storage (or another browser storage).

You can use this service if you want to make some data persistant :

ang.service('SessionStorageService', ['$window',function ($window) {
        var service = {
            store: store,
            retrieve: retrieve,
            clear: clear,
            clearAll: clearAll
        };

        return service;


        function store(key, value) {
            $window.sessionStorage.setItem(key, angular.toJson(value, false));
        }

        function retrieve(key) {
            return angular.fromJson($window.sessionStorage.getItem(key));
        }

        function clear(key) {
            $window.sessionStorage.removeItem(key);
        }

        function clearAll() {
            $window.sessionStorage.clear();
        }


    }]);
Sign up to request clarification or add additional context in comments.

Comments

1

Hi check out an earlier answer I gave to a similar question

Access ng-model data outside of the controller

The gist of it is to use a publish subscribe model.

with, for publishing $scope.$broadcast or $scope.$emit

and to subscribe $scope.$on

Comments

0

This can't work, if you have two separate ng-app directives on two different urls you are practicaly creating two completely seprate instances of the angular JS application that are complety isolated from one another.

Angular JS framework is here to create "single page applications" which is a bit different concept fro mprevious request based apps. What you need is single application and use a router to show different views (sub pages / urls) which are then appended after # in the url (can be changed by configuration fo router check html5mode)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.