AngularJs是空列表

时间:2016-11-20 08:48:59

标签: javascript angularjs asp.net-web-api asp.net-core

我使用.net Core和AngularJs + webApi制作简单的程序 我的Api代码如下 运行问题后没有Js错误是工厂返回什么都没有。 当我在$ location.qwets = index.query()上设置断点时;我的“qwets”是空的,“qwets”的长度是0。 每次页面刷新时get方法都在工作,但结果都没有。

  

我现在改变了代码我的结果是'qwets',但索引仍然是空的

谢谢

// GET: api/values
    [HttpGet]
    public IEnumerable<ApplicationUser> Get()
    {
        return new List<ApplicationUser> {
            new ApplicationUser {Id="test", Email="test1@test.com" },
            new ApplicationUser {Id="tst2",Email="test2@test.com" }

        };
    }

app.js文件

(function () {
'use strict';

angular.module('saniehhaApp', [
    // Angular modules 
    //'ngRoute'

    // Custom modules  
    "indexService"
    // 3rd Party Modules

]);
})();
(function () {
'use strict';

angular
    .module('saniehhaApp')
    .controller('indexController', indexController);

indexController.$inject = ['$location', 'index'];

function indexController($location, index) {
    index.query()
    .$promise.then(function (result) {
     $scope.qwets = result;
}
})();
(function () {
'use strict';
var indexService = angular.module('indexService', ['ngResource']);

indexService.factory('index', ['$resource',
    function ($resource) {
     return $resource('/api/index/', {}, {
         query:
             {
                 method: 'GET',
                 params: {},
                 isArray: true
             }
     });
}]);
})();

和我的index.html文件

<!DOCTYPE html>
<html ng-app="saniehhaApp">
<head>
<meta charset="utf-8" />
<title>SampleTest</title>

<script src="vendor/angular.min.js"></script>
<script src="vendor/angular-resource.min.js"></script>
<script src="vendor/angular-route.min.js"></script>
<script src="scripts/app.js"></script>
</head>
<body ng-cloak>
<div ng-controller="indexController"></div>
<h2>list of users</h2>
<ul>
    <li ng-repeat="qwet in qwets">
        <p> "{{qwet.Id}}" - {{qwet.Email}}</p>

    </li>
</ul>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您在控制器中使用$location而非$scope来分配视图中所需的属性。该视图在$location

中看不到任何内容

更改为

function indexController($scope, index) {
    /* jshint validthis:true */
    $scope.qwets = index.query();
}

如上面的评论中所述,$ resource最初将返回一个空数组(或对象),随后将在实际请求完成时填充该数组,内部观察者将在到达时更新视图

  

也是糟糕的结局&#34; div&#34;在HTML ng-repeat中不在div控制器中