2014-08-27 88 views
0

我是一個新手學習Angular。我使用Angluar在Aps.net MVC中創建了一個測試頁面,試圖從DB中檢索一些數據。檢索數據沒有問題。它恰好是Array的一個對象。但ng-repeat沒有顯示它。 我角控制器AngularJS不顯示數組對象

(function() { 

    var CompanyController = function ($scope,companyRepository) { 

     //another function which runs when http request completes. 
     var onGetCompaniesComplete = function (response) { 
      console.log(response); 
      $scope.companiesList = response; 
      alert($scope.companiesList.length); 
     }; 
     // on error 

     var onError = function (error) { 
      console.log('An error occured while getting companies list'); 
     }; 

     $scope.Message = "Hello to the world of Angularjs."; 

     //getting companies list. 
     companyRepository.get(). 
     then(onGetCompaniesComplete, onError); 

    }; 
    // registration the controller. 
    registrationModule.controller('CompanyController',CompanyController); 


}()); 

companyRepository

registrationModule.factory('companyRepository', function ($http, $q) { 
    return { 
     get: function() { 
      var deffered = $q.defer(); 
      $http.get('/CompanyNG/GetCompanies').success(deffered.resolve).error(deffered.reject); 
      return deffered.promise; 
     } 
    } 
}); 

Asp.Net控制器動作

public ActionResult Index() 
    { 
     return View(); 
    } 

    public string GetCompanies() 
    { 
     var companyModel = repository.Companies; 
     //var jsonResult = Json(companyModel, JsonRequestBehavior.AllowGet); 
     //return Json(companyModel, JsonRequestBehavior.AllowGet); 
     var settings = new JsonSerializerSettings { ContractResolver=new CamelCasePropertyNamesContractResolver() }; 
     var companies = JsonConvert.SerializeObject(companyModel, Formatting.None, settings); 
     return companies; 
    } 

查看

@section scripts{ 
    <script src="~/Scripts/Controllers/Repositories/CompanyRepository.js"></script> 
    <script src="~/Scripts/Controllers/CompanyController.js"></script> 
} 
<div ng-controller="CompanyController"> 
    {{Message}} 

    <table> 
     <thead> 
      <tr> 
       <th> 
        Name 
       </th> 
       <th> 
        Found 
       </th> 
       <th> 
        Office# 
       </th> 
       <th> 
        PSEB Registered 

       </th> 
      </tr> 
     </thead>   
     <tbody> 
     <tr ng-repeat="company in companiesList"></tr> 
     <td>{{company.companyName}}</td>   
     </tbody> 
    </table> 
</div> 

回答

1

像這樣

<tr ng-repeat="company in companiesList"> 
    <td>{{company.companyName}}</td> 
</tr> 
0

傢伙我覺得找到了癥結的問題。如果你看看視圖。該html沒有正確定義。

<tr ng-repeat="company in companiesList"></tr> 
<td>{{company.companyName}}</td> 

這應該是像下面

<tr ng-repeat="company in companiesList"> 
    <td>{{company.companyName}}</td> 
</tr> 

它解決了這個問題,我能看到頁面上的數據。