Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
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…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to count occurrences of each item in an array.

810 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