0

I would like to get my value from select ng-model="NumProdutos[$index]" if I click in the button.

The possibles values in this select is '1, 2, 3' and this select is inside a div with a ng-repeat

Here its my html code:

<select class="form-control"
        style="max-width:55%;"
        ng-model="NumProdutos[$index]"
        ng-options="obj.NumProdutos as obj.NumProdutos for obj in NumeroProdutos"
        ng-change="functionnumprodutos(NumProdutos[$index], {{x[0].NumPostoAgendamento}})"></select>

<input type="submit" value="teste" ng-click="teste($index)" class="btn btn-primary" />

Controller

$scope.teste = function (a) {
    console.log('a: ', a);

    console.log('N NumProdutos: ', $scope.NumProdutos[a]);
    console.log('N NumProdutos: ', $scope.NumProdutos);
    console.log('N NumProdutos: ', $scope.NumProdutos[$index]);
}

I tried console.log, but I didn't know how to get this values of ng-model="NumProdutos[$index]"

2 Answers 2

0

Try This :

HTML :

<select class="form-control"
    style="max-width:55%;"
    ng-model="NumProdutos[$index]"
    ng-options="obj.NumProdutos as obj.NumProdutos for obj in NumeroProdutos"
    ng-change="getIndex($index);functionnumprodutos(NumProdutos[$index], {{x[0].NumPostoAgendamento}})">
</select>

<input type="submit" value="teste" ng-click="teste(itemIndex)" class="btn btn-primary" />

Controller :

$scope.itemIndex = null;
$scope.getIndex = function (a) {
    $scope.itemIndex = a;
}

$scope.teste = function (a) {
    console.log('a: ', a);
    console.log('N NumProdutos: ', $scope.NumProdutos[a]);
    console.log('N NumProdutos: ', $scope.NumProdutos);
    console.log('N NumProdutos: ', $scope.NumProdutos[$index]);
}
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is $index is defined only for ng-repeat, not ng-options. So please proceed with following method, you will get the index in ng-change itself. Thank you

function LoginController($scope) {

    $scope.NumeroProdutos = [{"NumProdutos":1},{"NumProdutos":2},{"NumProdutos":3}];
    $scope.itemIndex = null;
$scope.getIndex = function (a) {
   console.log(a);
   console.log('N NumProdutos: ', $scope.NumeroProdutos[a]);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="LoginController">

<select class="form-control" ng-init="indexvalue"
    style="max-width:55%;"
    ng-model="NumProdutos" 
    ng-options="obj.NumProdutos for obj in NumeroProdutos"
    ng-change="getIndex(NumeroProdutos.indexOf(NumProdutos));">
</select>

</div>

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.