AngularJs: Pass input data to another route and then get it there

105 Views Asked by At

I'm trying to make SPA using angularJs routeProvider:

<script>
        var app = angular.module("myApp", ["ngRoute"]);

        app.config(function($routeProvider) {
            $routeProvider
            .when("/editnews/:newsId", {
                    template:   '<h2>{{msg}}</h2>'+
                                '<h2>{{newsId}}</h2>'+

                                '<form action="#!savechanges">'+
                                    '<table>'+
                                        '<tr><td>Title:     </td><td><input type="text" name="title"  value= {{news.title}} >                  </td></tr>'+
                                        '<tr><td>Brief:     </td><td><input type="text" name="brief"  value={{news.brief}}>                   </td></tr>'+
                                        '<tr><td>Date:      </td><td><input type="text" name="date"  value={{news.date|date:"dd/MM/yyyy"}}>   </td></tr>'+
                                    '</table>'+
                                '<button type="submit">Save</button>'+
                                '</form>',

                    controller: "editnews"
                })
                .when("/savechanges", {
                template:   '<h2>{{msg}}</h2>'+
                            '<h2>{{title}}</h2>'+
                            '<h2>{{brief}}</h2>'+
                            '<h2>{{date}}</h2>',


                controller: "savechanges"
            })
        });
        
        
 app.controller('savechanges', function($scope, $location) {
            $scope.msg = "Saved News";
            //get input value here

        });

How can I get input data from one route in the controller of another route. I tried to use $location.search() to get data, but it doesn't work. I think because input data placed in the middle of url: http://localhost:8888/?title=Title2&brief=2.4&date=10%2F09%2F2018#!/savechanges

1

There are 1 best solutions below

0
Tony On

u can use service to pass the data between controller, route, page etc.... sample here https://embed.plnkr.co/EZ5mN3GBIGoeaPyGQ6XM/