Sunday, 15 February 2015

javascript - Treat all variables from HTTP request as as integer (with exception) -



javascript - Treat all variables from HTTP request as as integer (with exception) -

i have http request in angular pulls info mysql table.

$http({ method: "post", url: "framework/actions/league.php?query=getdivision" }).success(function(data){ $scope.division = data; });

a snippet of info looks this:

[ { name: "someone", number: "4", game1: "6", game2: "2", score: "8" }, { name: "someone else", number: "7", game1: "7", game2: "3", score: "10" }, ]

when info comes http request , gets assigned $scope.division, numbers treated strings, gives me problems when using orderby in ngrepeat.

how can number fields treated numbers without having declare each field in foreach , utilize parseint()? possible in php? if not, doing in angular javascript fine.

of course of study can't treat every field number because of name field.

you can either convert strings numbers using php function intval() before sending result:

$string = '123'; $number = intval($string); // 123 $number2 = intval('456'); // 456

or casting variable directly on assign statement:

$string = '123'; $number = (int)$string; // 123 $number2 = (int)'456'; // 456

or in angularjs using transformresponse method:

$http({ method: "post", url: "framework/actions/league.php?query=getdivision", transformresponse: function(data) { (var i=0; i<data.length; i++) { for(var k in data[i]) { if (data[i].hasownproperty(k)) { data[i][k] = isnan(+data[i][k]) ? data[i][k] : +data[i][k]; } } } homecoming data; }, success: function(data){ $scope.division = data; } });

javascript php mysql angularjs

No comments:

Post a Comment