5

I have the following drop down using angularjs.

   <select ng-href="#/edit/{{name.id}}" class="form-control"  id="{{name.id}}">  
   <option ng-repeat="name in addas | filter:cc"
   id="{{name.id}}" value="{{name.name}}">{{name.as_number}}</option>
  </select>

I am trying to call href on selected option in the drop down. The drop down populates fine. When I select any option it doesn't do anything or call the url. Please let me know how I can achieve this in angularjs.

2 Answers 2

4
<select ng-model="adda" ng-options="a.name for a in addas" ng-change="onChange()">  
</select>

Sample controller:

function Ctrl($scope, $location) {
    $scope.addas = [
        {id: 1, name: 'a1', as_number: 1},
        {id: 2, name: 'a2', as_number: 2},
        {id: 3, name: 'a3', as_number: 3}
    ];

    $scope.onChange = function() {
        $location.path('/edit/' + $scope.adda.id);
    }
}

$location.path() does not seem to have any effect int the fiddle so instead the path is just displayed on the page but it should work as expected otherwise.


There is a number of things wrong with your approach. First of all you use ng-repeat on the option element so name.id won't be available outside of that element. It would probably be undefined when you use it in ng-href (unless you have it defined in the $parent scope as well but if I understand correctly this is not true).

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

4 Comments

I can't get this to work. See updated fiddle
Can you be more specific? I see a lot of paragraphs added to the html but no other changes. $location.path() has no effect inside the fiddle - thats for sure. I would guess it must be due to some restrictions imposed by fiddle itself. If I take the code from your updated fiddle and paste it in a new controller in a fresh project (generated with yeoman) then it works. The path is being changed to whatever I select. The path needs to be valid so I also configured a new route /edit/:id in app.js.
On selecting a1 in the dropdown it doesn't scroll to the paragraph with id 1. Don't think it's a problem with the fiddle, all I did was include extra paragraphs to show that it doesn't scroll to the required location. NB the question url is to a fragment (#/edit/...).
It was my understanding the question was how to manipulate the path in a simple way in general. If you would only want to change the hash fragment then you would indeed use $location.hash instead of $location.path. I'am afraid that in that case, again, if I create a simple demo it works locally but does not work in the fiddle. The internal state is being changed - getting the path or hash afterwards return the new values. It could be considered a serious bug in angular... but it works locally! Thus, fiddle issue.
2

The select statement looks a bit over complex to me and contains a few errors. Try this simpler version:

<select ng-model='adda'
        ng-options='as_number for name in addas' 
        ng-change='scrollToName()'>  
</select>

Controller:

$scope.scrollToName = function(){
    $location.hash($scope.adda.id);
    $anchorScroll()
};

Don't forget to inject $location and $anchorScroll into the controller.

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.