无法在角度js中实例化模块

时间:2014-07-29 05:14:40

标签: angularjs angularjs-factory

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

}]);

1 个答案:

答案 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>