2017-05-14 87 views
1

夥計們我有一個模型如下,從Ajax調用返回。AngularJS int映射不工作?

public partial class GroupForm 
{ 
    [Key] 
    public virtual int Id { get; set; } 
    public virtual int? GroupId { get; set; } 
    public virtual int? FormId { get; set; } 
    public virtual int? Frequency { get; set; }  
} 

在角Ajax調用我GroupForm的列表,然後用NG-重複展現FormID和頻率,而是表示頻率int值,我想顯示字符串,所以我做了映射,但它只顯示GroupId,頻率沒有顯示。 我的控制器部分如下:作爲folloed

FrequencyMap = { 
     1: "Annual", 
     2: "Monthly", 
     3: "Half Year" 
    } 

    $scope.mapFrequency = function (col) { 
     return FrequencyMap[col]; 
    }; 

而且我前angualrJS代碼:

<ul> 
       <li ng-repeat="w in GroupForm"> 
        {{ w.FormId+ ', '+mapFrequency(w.Frequency)}} 
       </li> 
      </ul> 

我使用這個代碼,Ajax調用返回三個GroupForm記錄,它只能顯示3的GroupId,但頻率字符串丟失。但是,如果我使用w.Frequency,它可以顯示int值。 請指點

我如何獲得GroupForm如下:

$scope.GetAllGroupForms = function (Id) { 
     var url = '/GroupForm/Details/' +Id; 
     $scope.loading = true; 
     DataSvc.getAjaxData(url) 
     .then(function (data) { 
      //success   
      $scope.GroupForm = data.data; 
      $scope.loading = false; 
     }, function (httpStatus) { 
      //failed 
      $scope.hasErrors = true; 
      if (httpStatus === 404) { 
       $scope.errorMessage = "Couldn't retrieve Users Info"; 
      } else { 
       $scope.errorMessage = 'The system could not process your request, please try again or contact the system administrator.'; 
      } 
      $log.warn(httpStatus); 
      $scope.loading = false; 
     }); 
    }; 
+0

mapFrequency後'缺少一個右括號的方式(w.Frequency' – chiliNUT

+0

是你的ng-repeat實際上工作嗎?你在視圖中獲得3個列表項... – Bowofola

+0

我添加了缺失的閉合parens,但仍然不起作用 –

回答

0

抽象邏輯到你的範圍。如果你這並不工作,那麼什麼是錯的或者與您的數據,或者你設置GroupForm

angular 
 
    .module('demo', []) 
 
    .controller('demoController', controller); 
 
    
 
function controller($scope) { 
 

 
    var frequencyMap = { 
 
    1: "Annual", 
 
    2: "Monthly", 
 
    3: "Half Year" 
 
    }; 
 
    
 
    $scope.GroupForm = [ 
 
    { 
 
     FormId: 123, 
 
     Frequency: 3 
 
    }, 
 
    { 
 
     FormId: 456, 
 
     Frequency: 1 
 
    }, 
 
    { 
 
     FormId: 789, 
 
     Frequency: 2 
 
    } 
 
    ]; 
 

 

 
    function getFrequency (frequencyId) { 
 
    return frequencyMap[frequencyId]; 
 
    } 
 

 
    $scope.formatGroupFormItem = function(groupForm) { 
 
    var frequency = getFrequency(groupForm.Frequency); 
 

 
    var result = groupForm.FormId; 
 
    if(frequency) { 
 
     result += ', ' + frequency; 
 
    } 
 

 
    return result; 
 
    }; 
 
}
<head><script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script></head> 
 
<body ng-app="demo" ng-controller="demoController"> 
 
    <ul> 
 
    <li ng-repeat="form in GroupForm"> 
 
     {{ formatGroupFormItem(form) }} 
 
    </li> 
 
    </ul> 
 
</body>

+0

非常感謝你的回答。我使用這個,但沒有顯示,即使formId消失了。它只顯示三個點,這意味着我猜三個記錄 –

+0

你的控制檯上是否有任何錯誤? – Bowofola

+0

不,Cuz我以前可以用Id和FormId獲得所有三個GroupForm。謝謝 –