0

I have all controllers defined at one place.

 $routeProvider.when(pages.hello, 
    {
      templateUrl: 'test/hello.html', 
      controller: 'myController',
      access: access.anon,
      helloPage: 'arf'
    });

My hello.html looks like this.

<select name="chooseFunc" data-ng-model="choosenFunction"  class="input-xlarge ng-pristine ng-valid" data-ng-click="fetchID()"  data-ng-change="update(choosenFunction)">
            <option value="ABCD">ABCD</option>
            <option value="CDEF">CDEF</option>            
        </select> 

Now I want to call a http get method "/abc/hello" to populate the drop down like ABCD, CDEF values based on it.

I have written some thing like this.

 $scope.fetchID = function() {
        console.log("hello in fectch id"); 
        $http.get('/abc/hello').success(successCallback).error(error);        
      };

My function is not getting called. Could some help me with the following.

  1. Calling fecthID() function on page load.
  2. fechID() function should populate the options in select element.

I am new here. Can some one help me.

2 Answers 2

1

I would write controller like:

function myController($scope,$http) {

    $scope.values = [{id:"ABCD",Name:"David"},{id:"CDEF",Name:"John"}];

    $scope.selectedItem = $scope.values[0].id;

     $scope.fetchID = function() {

        $http.get('/abc/hello').success(
            function(data, status, headers, config){
                $scope.values.push(data);// play here
            }) 
        .error(error);          
      };

 $scope.fetchID(); // call
}

and hello.html with init first combo value:

   <select name="chooseFunc"        
    ng-model="selectedItem"        
     ng-options="selectedItem.id as selectedItem.id for selectedItem in values"          
     class="input-xlarge ng-pristine ng-valid"          
     data-ng-click="fetchID()"          
      data-ng-change="update(choosenFunction)"          
      ></select>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for answer. My JSON object will look like this. can you also tell me how do I achieve this.[{"id":"ABCD","Name":"David"},{"id":"CDEF","Name":"John"}] And I want to display some thing like this <option value="ABCD">David</option> <option value="CDEF">John</option>
1

You can use ng-options to populate select like

<select ng-model="mySelecetdValue" ng-options="item for item in items">
</select>

To Call fecthID(), Just call it like

$scope.fetchID()

And Populate items in successCallback function

2 Comments

Thanks for replying. It would be great if you could also help me to call fetchID() function on page load and populate items.
@Patan, can you provide code for successCallback which you are using

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.