Solved

AngularJS - is the function returning usable JSON formatted data, and how to wire it to <select>

Posted on 2014-09-30
3
426 Views
Last Modified: 2014-10-20
//this function is supposed to return JSON data...

$scope.returnServiceType = function (stEnum) {
        var deferred = $q.defer();
        $http.get('/Ticket/ReturnServiceTypes', {
            params: {
                stEnum: stEnum
            }
        }).success(deferred.resolve).error(deferred.reject);

        return deferred.promise;
    }

 $scope.mTicketType = $scope.returnServiceType("calltype");

Open in new window





//This code is where the data above is bound to the <select> tag (combobox):

 <div class="ctrlRow">
        <label class="lbl">@Resources.ResStrings.TYPE:</label>        
         <select class="callSetting" ng-model="mTicketType"  ng-options="ticket.name for ticket in returnServiceType">
        </select>
    </div>

Open in new window



I've been stepping through the C# code behind, and as far as I can tell, the data is being converted over to JSON:

 public ActionResult ReturnServiceTypes(ServiceTypesEnum stEnum)
        {
            List<ServiceType> sTypes = ticketService.ReturnServiceTypes(stEnum);

            return Json(sTypes, JsonRequestBehavior.AllowGet); // set breakpoint here...
        }

Open in new window


Pasted in from the debugger Immediate Window at runtime:

((System.Web.Mvc.ContentResult)Json(sTypes, JsonRequestBehavior.AllowGet)).Content
"[{\"name\":\"Electrical\"},{\"name\":\"Installation\"},{\"name\":\"Jam\"},{\"name\":\"PM\"},{\"name\":\"CopyQuality\"},{\"name\":\"Receive-Send\"},{\"name\":\"Shop\"},{\"name\":\"Delivery\"},{\"name\":\"CourtesyCall\"},{\"name\":\"PhoneFix\"},{\"name\":\"eInfo\"},{\"name\":\"Not Working\"}]"

Open in new window







1)  How can I tell if the function is returning JSON data that the angular syntax can consume?
2)  Is the HTML properly wired-up to the Javascript via Angular commands?
3)  How can I debug this?  The JSON data seems to fall into a black hole.  I can't figure-out where to stop or what to look at in the Chrome F12 Developer Tools to verify that the function is returning JSON data...all I see is this:


get to JSON data?
0
Comment
Question by:Tom Knowlton
3 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 40352272
How can I tell if the function is returning JSON data that the angular syntax can consume?
If that's what you got from the Immediate Window, then it should be fine for Angular.

Is the HTML properly wired-up to the Javascript via Angular commands?
Hmmm. I don't know about this line:

<select class="callSetting" ng-model="mTicketType"  ng-options="ticket.name for ticket in returnServiceType">

Open in new window


Shouldn't that ng-options be:

ng-options="ticket.name for ticket in mTicketType"

How can I debug this? ...
Chrome or Firefox would be the preferred choices. You need to be focused on the service file itself. That is where you would want to put your breakpoint(s). That is, whichever file contains the "returnServiceType" code.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 40352943
replace
$scope.mTicketType = $scope.returnServiceType("calltype");

Open in new window

by :
$scope.returnServiceType("calltype").then(function(data) {
          $scope.mTicketType = data;
});

Open in new window


more info about promise here : https://docs.angularjs.org/api/ng/service/$q
0
 
LVL 5

Author Closing Comment

by:Tom Knowlton
ID: 40392376
thx
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question