Save an ng-repeat value and use it in another

Asked

Viewed 44 times

1

My question is the following. First I perform a search and fill my first select with ng-repeat. So far so good, but I perform another search for a second select that depends on this my first search. How do I get the value of that first ng-repeat?

Follows my codes

        <div ng-controller="ctrSecretariaPrograma">
      <label>Programa:<b><span style="color: red;">*</span></b></label></br>
      <select required ng-model="lista_programa_pos.model" class="form-control">
        <option value="">Selecione o Programa</option>
        <option ng-repeat="option in lista_programa_pos.availableOptions" value="{{option.cod_programa}}">{{option.nome_programa}}</option>
      </select>

      <label></br>Curso:<b><span style="color: red;">*</span></b></label></br>
      <select required ng-model="lista_curso.model" class="form-control">
        <option value="">Selecione o Curso</option>
        <option ng-repeat="option in lista_curso.availableOptions" value="{{option.cod_curso}}">{{option.nome_curso}}</option>
      </select>
    </div>

I need the value of the first select to perform the search in the second, follow my code js

      $scope.lista_programa_pos = {
    model: null,
    availableOptions: null
  };

  $scope.lista_curso = {
    model: null,
    availableOptions: null
  };

  var getProgramaPos = function () {
    $http.get("getProgramaPos").then(function (resposta) {
        $scope.lista_programa_pos.availableOptions = resposta.data;
    });

  }; getProgramaPos();


  var getCurso = function(option.cod_programa){
    var data = {
      cod_programa: option.cod_programa
    };

    $http.get("getCurso", data).then(function(resposta){
      $scope.lista_curso.availableOptions = resposta.data;
    });

  }; getCurso(option.cod_programa);

I don’t know if I’m calling a certain function, because one depends on the other and I also don’t know how to get this value in the getCurso part (option.cod_programa), where would be the code I keep in my first ng-repeat

1 answer

1


RESOLVED!

I used ng-change to call my getCurso() function:

          <select required ng-model="lista_programa_pos.model" class="form-control" ng-change="getCurso(lista_programa_pos.model);">
            <option value="">Selecione o Programa</option>
            <option ng-repeat="option in lista_programa_pos.availableOptions" value="{{option.cod_programa}}">{{option.nome_programa}}</option>
          </select>

My js file:

$scope.getCurso = function(cod_programa){

console.log(cod_programa);
var data = {
  cod_programa: cod_programa
};

$http.post("getCurso", data).then(function(resposta){


     $scope.lista_curso.availableOptions = resposta.data;
    });

  };

Removing the var and adding $Scope and removing the automatic function call

Browser other questions tagged

You are not signed in. Login or sign up in order to post.