DIVA
DIVA

Reputation: 579

Passing the success message from one view page to another page using angularJS

I am new to angularJS. I have the requirement like i want to pass the success message from one html page to another page and I want to print that success message.

Here is my angularJS controller code,

$http({
                method: 'POST',
                url: '/test/resources/rest/event/createEvent',
                headers: {'Content-Type': 'application/json'},
                data:  $scope.event
            }).success(function (data){
                if(data.status.status == 200){
                    //alert("data.status.message:"+data.status.message);
                    $scope.message=data.status.message; 
                    $window.location.href = "/test/view/myEvents.html";
                }else{
                    $scope.message=data.status.message;
                }
            });

The success message is binded under the scope.message.Just i want to pass that message and need to get in another page to print.

Please help me

TIA...

Upvotes: 0

Views: 1587

Answers (1)

pedrommuller
pedrommuller

Reputation: 16056

you can use $rootscope to pass messages from one controller to another or even better to emit an event, that's as easier as:

    $http({
            method: 'POST',
            url: '/test/resources/rest/event/createEvent',
            headers: {'Content-Type': 'application/json'},
            data:  $scope.event
        }).success(function (data){
            if(data.status.status == 200){
                //alert("data.status.message:"+data.status.message);
                $rootScope.message=data.status.message; 
                $location.path('YourRoute');
            }else{
                $scope.message=data.status.message;
            }
        });

you can also use a service, an event bus, etc. that really depends on your needs but the easier is to use rootscope.

EDIT:

you can reference directly to rootscope on a view using {{$root.message}} look at this fiddler for reference http://jsfiddle.net/3XT3F/10/

Upvotes: 1

Related Questions