1

I have index.html file which have these following below codes. I want to show result to other view searchResult.html when submitting the form. I retrieve the form data in homeCtrl but i can't do when submitting the form show the retrieved data to searchResult.html page. Please help me.

<!DOCTYPE html>
  <html ng-app="myApp">
      <head>
          //head code...
      </head>
      <body  ng-controller="homeCtrl">
        <form  ng-submit="doSearch()">
          <input type="search" placeholder="From" ng-model="search.from">
          <input type="search" placeholder="Destination" ng-model="search.to">
          <button type="submit">Search</button>
      </form>

      <div ng-view></div>
    </body>
 </html>


 var app = angular.module('myApp', [])
 app.controller('homeCtrl', function($scope, $location) {
     $scope.doSearch = function() {
        $scope.search={};
        $scope.search=$scope.search;
        //retrieved data here
     } 
 });

2 Answers2

0

You can get the data in homeCtrl and store it in rootscope or localstorage then redirect page to search page and retrieve data from rootscope or localstorage to searchCtrl scope.

Sarjan Desai
  • 3,683
  • 2
  • 19
  • 32
0

It may be tempting to pass data around through the $rootScope, but this is problematic. To start, your data is now bound to the root scope, and can’t be moved off into isolation. Not only is this harder to test, but it can’t be used through multiple applications if need be.

  1. take out your Searching logic in Separate Service.

  2. use ui-router for URL routing. Following links will be helpful!

navigation with routing -part1

navigation with routing -part2

rupesh_padhye
  • 1,355
  • 2
  • 13
  • 25