1

I am trying to retrieve a value from my view to controller.The value is generated at runtime.

<ng-option ng-repeat="w in details track by $index">
    <div class="program-grid-row table-row" >
      <div>
          <a class= ng-click="setWork('hello')" 
                  href="#/helloworls/{{w[5]}}">{{w[5]}}</a>
      </div>
    </div>
</ng-option>

I want to retrieve the value of w[5] at my controller such that controller.js

$http.get(Url +$scope.w[5]).then(function(response){
  $rootScope.details= response.data;
});

Not sure how can we retrieve the value of w[5] at the controller. Pls help.

2
  • 1
    Your logic is incorrect and unclear. You are trying to get 6th element of w for all the iteration over details . and then what are you doing in $http get ? Doesnt make any sense. Do you want to make that $http call in setWork function ? so that a http request is made for the clicked <a href> Commented Apr 20, 2018 at 8:58
  • Check my answer where I have provided answer for both possibility Commented Apr 20, 2018 at 9:00

2 Answers 2

1

You can try to get w[5] of the last value of $scope.details but I think your question is not correct in itself:

<ng-option ng-repeat="w in details track by $index">
<div class="program-grid-row table-row" >
  <div>
      <a class= ng-click="setWork('hello')" 
              href="#/helloworls/{{w[5]}}">{{myData = w[5]}}</a>
  </div>
</div>
</ng-option>

in controller

$http.get(Url +$scope.myData).then(function(response){
   $rootScope.details= response.data;
});

OR

<ng-option ng-repeat="w in details track by $index">
<div class="program-grid-row table-row" >
  <div>
      <a class= ng-click="setWork(w[5])" 
              href="#/helloworls/{{w[5]}}">{{w[5]}}</a>
  </div>
</div>
</ng-option>

in controller

$scope.setWork = function(data){
   $http.get(Url +data).then(function(response){
      $rootScope.details= response.data;
   });
 }
Sign up to request clarification or add additional context in comments.

Comments

0

You can pass the w[5] as a parameter in ng-click function and get that clicked value in the controller:

VIEW

<ng-option ng-repeat="w in details track by $index">
      <div class="program-grid-row table-row" >
      <div>
      <a class= ng-click="setWork(w[5])"
    href="#/helloworls/{{w[5]}}">{{w[5]}}</a>
    </div>
    </div>
</ng-option>

Controller

$scope.setWork = function(param){
  //param has `w[5]` value which is clicked
  $http.get(Url + param).then(function(response){
    $rootScope.details= response.data;
  });
}      

3 Comments

But my details has multiple rows.How will it switch automatically to the selected row and further to the fifth field of that row
@Pall did you ask how to retrieve the 5th value in controller? That is how you do it
@Pall check my updated answer that should surely work for you

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.