0

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);
}

}]);
4

1 回答 1

1

在 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>
于 2014-07-29T05:20:07.420 回答