无法使用ajax调用从asp.net mvc控制器获取数据到angularjs

时间:2017-01-10 12:36:09

标签: angularjs ajax asp.net-mvc

我能够点击控制器方法并能够返回数据

[HttpPost]
    public JsonResult GetAppsList()
    {          
        var Apps = DataModel.ApplicationMasters.ToList();
        return Json(new { appslist = Apps }); 
       // return AppsList;
    }

但在角度js中无法获取数据

myApp.controller('SpicyController', ['$scope','$http', function ($scope,$http) {    
$http({
    method: 'POST',
    url: 'Home/GetAppsList'
}).then(function (response) {
$scope.appslist = response.data.appslist; }, function (error) {console.log(error);});}]);

在视图中

<div class="main" ng-app="spiceApp">
    <div ng-controller="SpicyController"> 
        <table>          
            <tr ng-repeat="app in appslist">
                <td>
                    {{app.Name}}
                </td>
            </tr>
        </table>
       </div>
    </div>
你能帮我解释为什么我无法显示结果吗?

1 个答案:

答案 0 :(得分:1)

<div class="main" ng-app="spiceApp">
<div ng-controller="SpicyController"> 
    <table>          
        <tr ng-repeat="(index,data) in appslist">
            <td>
                {{data.Name}}
            </td>
        </tr>
    </table>
   </div>
</div>

applist是一个对象。要在对象上应用ng-repeat,请写下ng-repeat=(index,data) in applist

myApp.controller('SpicyController', ['$scope','$http', function ($scope,$http) {    
$http({
    method: 'POST',
    type:'json',
    url: 'Home/GetAppsList'
}).then(function (response) {
$scope.appslist = response.data.appslist; }, function (error) {console.log(error);});}]);