I make simple program with .net Core and AngularJs + webApi My Api code as below There is no Js error after running the problem is factory return nothing. when I set break point on $location.qwets = index.query(); my "qwets" is empty the length of "qwets" is 0. The get method is working each time page refresh but result is nothing.
I changed the code now I have results in 'qwets' but index is still empty
Thank you
// GET: api/values
[HttpGet]
public IEnumerable<ApplicationUser> Get()
{
return new List<ApplicationUser> {
new ApplicationUser {Id="test", Email="[email protected]" },
new ApplicationUser {Id="tst2",Email="[email protected]" }
};
}
app.js File is
(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
}
});
}]);
})();
and my index.html file
<!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>
$resourcebehavior. The request is asynchronous so$resourceinitially returns an empty array which then gets populated when request completes. Unless you need to do something immediately in the controller when data arrives it shouldn't be a problem...watchers in the view would still update things like ng-repeat$locationand not$scope