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>