2

Just started trying out angular js. I have setup the standard asp.net mvc api controller:

public class ValuesController : ApiController
{
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
}

I am trying to get those values in my listbox:

<div ng-controller="myctrl">
        <select ng-model="selectedValue" ng-options="val for val in getData()">
            <option value="">Select</option>
        </select>
</div>

Using this script:

 var app = angular.module('app', []);
 app.controller('myctrl', function ($scope, $http) {
     $scope.getData = $http.get('/api/values').then(function (data) {
         return data;
     });
 });

When I run this it does not populate the listbox and jsfiddle:http://jsfiddle.net/dingen2010/t3eXj/

0

2 Answers 2

2

Promise unwrapping does not happen in AnuglarJS 1.2.x version. I believe your code should do something like

     $http.get('/api/values').then(function (data) {
         $scope.values=data;
     });

And in your html

        <select ng-model="selectedValue" ng-options="val for val in values">
            <option value="">Select</option>
        </select>
Sign up to request clarification or add additional context in comments.

Comments

0

In your use case above, had you of chosen to chain your get call with the success method instead, then data would be what you expect it to be (i.e. the data returned by your API).

But, since you are using the then method, the argument fed to the callback is actually a promise object, and the data you want can be accessed using the data property.

 $scope.getData = $http.get('/api/values').then(function (promise) {
     return promise.data;
 });

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.