0

I have a select list:

<select name="forma" onchange="location = Category(SCID: this.value)">
     <option value="38">Home</option>
     <option value="39">Contact</option>
     <option value="40">Sitemap</option>
</select>

On selecting a option i want to make it equivalent to http://localhost/#/Category/38/.

How to do that?

i tried this: location = Category(SCID: this.value)

But its giving error :

Uncaught SyntaxError: missing ) after argument list

2 Answers 2

2

I understand that you want to write the code inline and change the location. My advise to you is don't.

When having the option of calling a function and writing javascript code inline, go for calling a function. It keeps your code clean, easier to understand and easier to maintain.

Thus, in this case, attach a model to you select so that when the user selects a value, you know which value the user has selected:

<select name="forma" data-ng-model="selectedFormaValue" 
        data-ng-change="updateLocation()">
     <option value="38">Home</option>
     <option value="39">Contact</option>
     <option value="40">Sitemap</option>
</select>

Notice that I have attached a ng-change directive which calls the function updateLocation() when the select value changes.

Your updateLocation() function should look like:

$scope.updateLocation = function () {
    //Make sure that a value is selected
    if ($scope.selectedFormaValue) {
        $location.path('/Category/' + $scope.selectedFormaValue);
    }
};

This will cause the page to go to the location http://locahost/#!/Category/<SelectedValue>

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

2 Comments

Thanks for your very quick response. One more thing how to go to location: localhost/#/Category/<Selectedvalue> instead of locahost/#!/Category/<SelectedValue>
The #! depends on your route configuration. You can set html5mode to true or specify your own hashbang equivalent. Google "angularjs html5mode" for more info.
0

I dont think there is any onchange attribute to select directive in angularjs. You can use ngChange as stated by callmekatootie. You can try to do it inline as you are doing it now, or you can bind it to a scope function in controller. You should always delgate dom manipulations to controller which inturns changes the model. Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.