1

I am giving my first steps with Angular.js and I am facing a little problem which I don't know how to deal with Angular. I know many jQuery ways for solving it, but I am sure that it may exists an Angular way:

Firstly, I have an select element set with currentPlant as ng-model

  <select class="form-control" name="planta" ng-model='currentPlant' ng-options='plant.name for plant in plants' >
  </select>

I print the current name as page title, I see this element updating when I change the selected value, so I am sure that currentPlant is being correctly updated.

<h1>{{ currentPlant ? currentPlant.name : 'Seleccione una planta'}}</h1>

Now I have the following button:

<button class='btn btn-primary' ng-click='loadTowers()'>
  Cargar torres
</button>

When the button is pressed, the loadTowers function is executed, and is now when the problems start. I have set the loadTowers function in the controller that way:

$scope.loadTowers = plants.towers;

plant is a angular service which has implemented the towers function inside. I need to get the value of currentPlant inside this function. Take a look:

.factory('plants', ['$http',function($http){
  var o = {
    plants: [],

    towers : []
  };


  o.towers =  function(){
     var id = ¿?; // I NEED SET THIS VARIABLE WITH THE currentPlant.id VALUE 
     $http.get('/api/v1/plants/'+id+'/towers')
     .success(function(data){
         angular.copy(data, o.towers);
      })
     }
    return o;
   }]);

I could set an data-plant attribute in the option element, and get the selected through jQuery, but I am trying avoid this kind of solutions. How do you deal with this?

NOTE: Take account that I simply want get the option selected when I press the button and make different get requests to the API`in function of that through the service.

Thanks in advance.

1 Answer 1

2

Pass the value in the ngclick:

     <button ng-click='loadTowers(currentPlant)'>

Then add it in the factory args:

    o.towers =  function(currentPlant){

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

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.