Solved

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

Posted on 2014-09-30
3
407 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 74

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 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

11 Experts available now in Live!

Get 1:1 Help Now