Angularjs无法处理C#Web服务数组

时间:2017-02-17 03:14:10

标签: angularjs arrays

我写了一个返回json数组的Web服务。以下函数返回List中的数据:

List<ValPair> result = _service.getAllGroupsTypes(P_ClientID);
        var jsonSerial = new JavaScriptSerializer();
        var jsonResult = jsonSerial.Serialize(result);
        return Json(jsonResult, JsonRequestBehavior.AllowGet);

客户端代码:    $ scope.getAllGroups = function()     {

    var Parameters = { "P_ClientID": 1 };
    $http.post("/SMS/getAllGroups", Parameters)
    .then(function success(response) {
        $scope.received = response.data;
        $scope.names = [{ "name": "G1", "value": "1" }] //Just for test purpose
    })
}

在客户端,BATARANG将范围变量显示为:

received: [{"Key":"Default Group","Value":"1"}]
names: 
   [  { 
       name: G1
       value: 1
    }  ]

Angular正在处理ng-repeat中的$ scope.names,其中没有显示$ scope.received的任何结果。由于两者都是阵列,&#34;名称&#34;是从db中检索到的硬编码。

1 个答案:

答案 0 :(得分:0)

只需将您的代码更改为:

$calendarId1 = 'meetingroom1@resource.calendar.google.com'; 
$calendarId2
 = 'meetingroom2@resource.calendar.google.com';
$calendarId3 = 'meetingroom2@resource.calendar.google.com';
$optParams = array(
  'maxResults' => 10,
  'orderBy' => 'startTime',
  'singleEvents' => TRUE,
  'timeMin' => date('c'),
);

$results1 = $service->events->listEvents($calendarId1,$optParams);
$results2 = $service->events->listEvents($calendarId2,$optParams);
$results3 = $service->events->listEvents($calendarId3,$optParams);

$results=array($results1->getItems(),$results2->getItems(),$results3->getItems());


//declaration of color row counter to modulus
$counter=0;
$meetingticker=0;

if((count($results1) == 0)&&(count($results2) == 0)&&(count($results3) == 0))
{
  $meetingticker++;
  echo "room is free";
} else 
{

  $i=0;

  while($i<=2)
  {
    foreach($results[$i] as $event)
    {
    $start = $event->start->dateTime;
    $end = $event->end->dateTime;
    if (empty($start)) {
      $start = $event->start->date;
    }
    $dateStart = date("jS F Y", strtotime($start));
    $timeStartinU = strtotime($start);//date("H:i", strtotime($start));
    $timeEndinU = strtotime($end);//date("H:i", strtotime($end));
    $timeStart= date("H:i", strtotime($start));
    $timeEnd = date("H:i", strtotime($end));
    $lokasi = $event['location'];
    $meeting = $event->getSummary();

    if (($timeStartinU<= $currentTime)&&($timeEndinU>= $currentTime))
    {

    //show date summary location in blue color

              $counter++;
    }
    else
    {
     //show date summary location in dark blue color            
              $counter++;
    }
  }$i++;
}

输出将是:

var Parameters = { "P_ClientID": 1 };
$http.post("/SMS/getAllGroups", Parameters)
.then(function success(response) {
    $scope.received = JSON.parse(response.data);
    $scope.names = [{ "name": "G1", "value": "1" }] //Just for test purpose
})