Skip to content Skip to sidebar Skip to footer

Common Filter For Two Different Routes

var app = angular.module('myApp', ['ngRoute', 'ngSanitize']); app.controller('landingPageController', function($scope) { $scope.countries = [{ name: 'India', Id: '1' }

Solution 1:

You can share data between different controllers (or different instances of same controller) using e.g. services. So in your scotchApp (sounds tasty, BTW!) you could have something like

scotchApp.service('countryService', function() {
  var current;

  return {
    // set selected countryset: function(country) {
      current = country;
      console.log('set', current);
    },
    // get selected countryget: function() {
      console.log('get', current);
      return current;
    }
  };
}); 

And your mainController as

scotchApp.controller('mainController', function($scope, countryService) {
  $scope.countries = [{
    name: 'India',
    id: 1// note the property casing here
  },{
    name: 'Nepal',
    id: 2
  }];

  // get selected country      $scope.selectedCountry = countryService.get();

  // set selected country$scope.set = function(country) {
    countryService.set(country);
  };
});

And the template for your routes

<div><selectng-options="country.name for country in countries track by country.id"ng-model="selectedCountry"ng-change="set(selectedCountry)"><optionvalue="">Select country</option></select></div>

That should do it.

Post a Comment for "Common Filter For Two Different Routes"