[英]Factory function gives "Error: is undefined
Im having trouble making a http.get call to my api. 我无法对我的api进行http.get调用。 Here is the setup:
这是设置:
user.js user.js的
.config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/hem');
$stateProvider.state('anvandare', {
url: '/anvandare',
views: {
'main': {
controller: 'UserController',
templateUrl: 'modules/user/user.tpl.html'
}
}
});
}])
.controller('UserController',
['$scope', '$http', 'userFactory', function($scope, $http, userFactory) {
this.users = [{name: "Patrik"}];
$scope.message = userFactory.test();
userFactory.getUsers().then(function(users) {
$scope.users = users;
});
}])
.factory('userFactory', ['$http', function($http) {
return {
test: function() {
return "hello world";
},
getUsers: function() {
$http.get('/api/user');
}
};
}]);
modules/user/user.tpl.html 模块/用户/ user.tpl.html
<div class="container" ng-controller="UserController as UserCtrl">
<h1>Users</h1>
<p>{{message}}</p>
<ul>
<li ng-repeat="user in users">{{user.name}}</li>
</ul>
</div>
So right now, I have cache disabled and Im seeing no 304:s on the request so I know all the responses are fresh, and I can see the <h1>Users</h1> <p>{{message}}</p>
and message is = "hello world" as it should, but the list of users is not showing up. 所以现在,我已禁用缓存,我在请求中看不到304:s所以我知道所有响应都是新鲜的,我可以看到
<h1>Users</h1> <p>{{message}}</p>
和message is =“hello world”应该如此,但是用户列表没有显示出来。 As I look i chrome tools on the page it says 当我看到页面上的镀铬工具时,它说
"Error: userFactory.getUsers(...) is undefined"
When type 'localhost:5000/api/user' in my browser I do get all users back in json 在我的浏览器中输入'localhost:5000 / api / user'时,我确实让所有用户都回到json中
[{"_id":"54e4c44f8d18e2a43f58935f","name":"Patrik","__v":0,"updated_at":"2015-02-18T16:56:47.105Z"},{"_id":"54e4c4588d18e2a43f589360","name":"Patrik Nygren","__v":0,"updated_at":"2015-02-18T16:56:56.603Z"},{"_id":"54e4c45f8d18e2a43f589361","name":"Patrik Ackerfors","__v":0,"updated_at":"2015-02-18T16:57:03.011Z"},{"_id":"54e4c4888d18e2a43f589362","__v":0,"updated_at":"2015-02-18T16:57:44.226Z"}]
What am I doing wrong? 我究竟做错了什么?
you must return something from your getUsers
method: 你必须从你的
getUsers
方法返回一些东西:
getUsers: function() {
return $http.get('/api/user');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.