UserServices.js
angular.module('UserService', ['ngResource']).factory('UserFactory', ['$http', function() {
return {
// call to get all nerds
get : function() {
return $http.get('/api/User');
},
// call to POST and create a new geek
create : function(userData) {
return $http.post('/api/User', userData);
},
// call to DELETE a geek
delete : function(id) {
return $http.delete('/api/User/' + id);
}
}
}]);
UserCtrl.js
angular.module('UserCtrl', ['UserFactory']).controller('UserController',
['$scope','UserFactory', function($scope, UserFactory) {
$scope.insert = function(){
$scope.fromfactory = UserFactory.create($scope.user);
}
}]);
答案 0 :(得分:1)
在UserCtrl中,您需要检索模块,而不是重新定义它:
<强> UserCtrl 强>
angular.module('UserService').controller('UserController'...);
这是模块的正确结构:
<强> JS 强>
var app = angular('app', ['ngResource']);
app.factory('UserFactory', function() { ... });
app.controller('UserCtrl', function($scope) {...});
<强> HTML 强>
<body ng-app='app'>
...
</body>