Solved

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

Posted on 2014-09-30
3
414 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: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:knowlton
ID: 40392376
thx
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

919 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now