0

User from front page ,select the city from below list and as per search product are displayed.

But after the every product displayed , page reload and City selected get changed to First city and user need to select again city for every search.

Need help how to overcome this issue and set the city to user selected ,not refreshed every time .

//Define an angular module for our app
var app = angular.module('myApp', ['ui.bootstrap']);

app.controller('autocompleteController', function($scope, $http) {

    $scope.cities = [
        {name: 'London'},
        {name: 'Paris'},
        {name: 'Newyork'},
    ];
    $scope.selectedCities = $scope.cities[0];
    var initalizeproduct = function() {
        $scope.products = $http.get("/getproduct/"+$scope.selectedCities.name).success(function(data){
            $scope.products = data.products;
        });
    }
    initalizeproduct($scope.products);
    $scope.changeCity = function() {
        initalizeproduct();
    }
    $scope.onSelectPart = function (item) {
        $scope.selectedproducts = item;
        window.location.href = "/product/"+$scope.selectedproducts.fields.slug;
    };

});

From HTML Page :

<select ng-model="selectedCities" ng-change="changeCity()" ng-options="cities.name for cities in cities" style="background :transparent; border:0px; outline: none;">
            </select>
Steve
  • 1,159
  • 10
  • 14
Manoj
  • 11
  • 3

1 Answers1

0

Replace window.location.href = "..."; with:

$http
    .get("/product/"+$scope.selectedproducts.fields.slug)
    .then(function( ̶d̶a̶t̶a̶ response) {
       var data = response.data; 
       // Get the HTML part from 'data' that you would like to display and add it to DOM
    });

to begin with.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Brainfeeder
  • 2,604
  • 2
  • 19
  • 37
  • 1
    The `.success` method is deprecated and [removed from V1.6](https://stackoverflow.com/questions/35329384/why-are-angular-http-success-error-methods-deprecated-removed-from-v1-6/35331339#35331339). – georgeawg Jan 17 '18 at 15:30
  • Does `.then` work? Honestly.. I'm not an Angularjs expert :) – Brainfeeder Jan 17 '18 at 15:45
  • The `.then` method returns a response object of which `data` is one of its properties. For more information, see [AngularJS Developer Guide - Migrating to V1.6 - http](https://code.angularjs.org/snapshot/docs/guide/migration#migrate1.5to1.6-ng-services-$http) – georgeawg Jan 17 '18 at 15:59