AngularJS不显示对象数组

时间:2014-08-27 17:13:18

标签: angularjs asp.net-mvc-4

我是学习Angular的新手。我使用Angluar在Aps.net MVC中创建了一个测试页面,尝试从DB中检索一些数据。检索数据很好。它碰巧是Array的一个对象。但ng-repeat没有显示出来。 我的角度控制器

    (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>

2 个答案:

答案 0 :(得分:1)

喜欢这个

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

答案 1 :(得分:0)

伙计们我认为找到问题的原因。如果你看看。 html没有正确定义。

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

应该如下所示

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

它解决了问题,我能够在页面上看到数据。