0

I'm can't access the value of the select in the controller when the select is inside an ng-repeat.

Here is the sample of Html (Everything display properly):

<tr ng-repeat="reservationItem in dataList">
    <select ng-model="detail" ng-change= "haha($index)" ng-options="n for n in [1,2,3,42]" >
        <option value="Choose"> Choose </options>
    </select> 
</tr>

``

In the controller, inside the function haha() I try to get the value of the selected dropdown: $scope.haha = function(index){ console.log(index); console.log($scope.detail); }

The first console.log always show 0 The second console.log always shows undefined

Can someone tell me how to access the value of a select inside a nested from a controller. ? Thanks

5
  • your ng-model should be relative to your iteration in the ng-repeat...reservationItem.detail should be it's own parameter Commented Nov 16, 2014 at 6:12
  • But in this case as you explained, how do you access the value from the controller ? I tried $scope.reservationItem.detail andI got an error saying " TypeError: Cannot read property 'reservationDetail' of undefined". Commented Nov 16, 2014 at 13:31
  • reservationItem is just an alias you're using in the ng-repeat attribute for your dataList collection. $scope.dataList is your parent object...you'd be binding to a property of a member of the dataList array Commented Nov 17, 2014 at 5:09
  • yeah...and be sure to use reservationItem.detail for your ng-model Commented Nov 17, 2014 at 5:11
  • and haha($index) appears to be providing you with what index you want to query on the action...pass reservationItem instead of $index and you'd have your whole object with the chosen detail loaded Commented Nov 17, 2014 at 5:12

1 Answer 1

0

In this case, you have multiple detail ng-model. This confuses the angular, look at ng-model like an ID. you can't have duplicate. You would need to pull the select out, or attached detail to reservationItem ie. reservationItem.detail

Also, you won't need ng-change as angular autobinds that value to the model.

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

2 Comments

But in this case as you explained, how do you access the value from the controller ? I tried $scope.reservationItem.detail andI got an error saying " TypeError: Cannot read property 'reservationDetail' of undefined".
reservationDetail is actually a private variable, only known to the function. You will need to send that variable during a function call. Do something like this <select ng-model="reservationItem.detail" ng-change= "haha(reservationItem.detail)">

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.