2

I want to redirect to another page depending on the option chosen, but it's not even console logging. I tried using ng-change too

HTML:

      <select ng-options="option for option in selectCate" ng- 
      model="selectedCate" ng-click="selectedCateChange()">
      </select>

JS:

      $scope.selectCate = ['Amenities', 'Attractions'];

      $scope.selectedCate = $scope.selectCate[0];

      $scope.selectedCateChange = function () {

          if ($scope.selectedCateChange == "Amenities")
          {
              console.log("aa")
          }
          else if ($scope.selectedCateChange == "Attractions")
          {
             $window.location.href = '#!/Attractions'
          }
      }
4
  • Are you using routing. Commented Oct 23, 2018 at 6:11
  • Yup i am @SudhirOjha Commented Oct 23, 2018 at 6:20
  • Use $state.go("your route name"); instead of $window.location.href and make sure injecting $state in your controller. and change $scope.selectedCate = $scope.selectCate[0]; to $scope.selectedCate = $scope.selectCate; Commented Oct 23, 2018 at 6:29
  • Not working, it doesn't seem to be reacting any all. Can't even console log Commented Oct 23, 2018 at 6:47

2 Answers 2

2

Instead of using ng-click on <select> use ng-change and pass the selectedCate in selectedCateChange. Please consider the following working code snipet.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
<select ng-model="selectedCate" ng-change="selectedCateChange(selectedCate)">
      <option ng-repeat="option in selectCate" value="{{option}}">{{option}}</option>
</select>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.selectCate = ['Amenities', 'Attractions'];
    $scope.selectedCateChange = function (selectedCate) {
       if (selectedCate == "Amenities"){
           alert("aa");
       }else if (selectedCate == "Attractions"){
           alert("call your router here");
       }
   }
});
</script>
</body>
</html>

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the help! :)
Just one more question, is it possible to let the option stay as it is when the page is changed? For example, i select attractions, and on the attractions page the option list would stay at attractions and not go back to amenities
Yes you can make one value default selected using ng-selectedon option tag.
Uhh, i just looked up on ng-selected, i don't think thats what I want. I am not trying to initialize a value, I want it to stay as according to the choice picked
@HangulSR happy to help you.
0

For Redirect the angular url can you use the $stateProvider.

$stateProvider
            .state("Attractions", {
                url: "/Attractions",
                templateUrl: "app/components/layout/Attractions.html",
                resolve: loader(["scripts/components/layout/AttractionsController", "scripts/components/distributed/popupModalController"]),
                controller: "attractionsCtrl"
            });
$state.go('Attractions');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.