Javascript 无法在控制器中注入工厂
我对angulerJS是新手。我已经定义了一个工厂来从API获取数据,但是当我试图将工厂放在控制器中时,我得到了一个错误 这是工厂代码Javascript 无法在控制器中注入工厂,javascript,angularjs,Javascript,Angularjs,我对angulerJS是新手。我已经定义了一个工厂来从API获取数据,但是当我试图将工厂放在控制器中时,我得到了一个错误 这是工厂代码 (Function () { var CategoriesFactory = function($http) { var factory = {}; factory.getCategorys = function(account_id){ return $http.get('http://loc
(Function () {
var CategoriesFactory = function($http) {
var factory = {};
factory.getCategorys = function(account_id){
return $http.get('http://localhost:18678/api/Transaction?account_id=2');
};
factory.getTransaction = function(acc_id){
return $http.get('http://localhost:18678/api/Transaction?acc_id=2');
};
factory.getTransactionInCategory = function(category_id, from_date, to_date){
return.$http.get('http://localhost:18678/api/transaction?category='+category_id+'&account=2&from=2015-01- 01&to=2015-12-30');
};
return factory;
};
angular.module('AccApp').factory('CategoriesFactory', CategoriesFactory);
}());
这是控制器
app.controller('CategoriesController',
function ($scope, $routeParams, $http, CategoriesFactory) {
})
这里是错误。
未知提供程序:CategoriesFactoryProvider我猜您一定忘了将AccApp注入您定义应用程序的mai模块中
angular.module("app", ['AccApp']);
请像这样做
希望这有帮助 为什么你要尝试用一种更灵活的方式编写角度代码? angular的目标是简单
var app = angular.module('AccApp',[]);
app.factory('CategoriesFactory',function($http){// you can cut and paste this factory into a seperate file if you wish
return{
getCategorys:function(account_id){
return $http.get('http://localhost:18678/api/Transaction?account_id=2');
},
getTransaction:function(acc_id){
return $http.get('http://localhost:18678/api/Transaction?acc_id=2');
},
getTransactionInCategory : function(category_id, from_date, to_date){
return.$http.get('http://localhost:18678/api/transaction?category='+category_id+'&account=2&from=2015-01- 01&to=2015-12-30');
};
}
});
现在,您只需将此工厂注入控制器:
app.controller('CategoriesController',function ($scope, $routeParams, $http, CategoriesFactory){
console.log(CategoriesFactory.getCategorys(getCategorys));
});
您的
app
模块名为AccApp吗?是,它名为AccApp@user2918388:请显示所有相关代码:如何初始化模块,etcvar app=angular.module(“AccApp”,[])的错误跟踪是什么;这就是我写的我正试图在一个不同的文件中分离工厂代码好吧,但按我的方式做吧,现在你最好能把这个工厂剪切粘贴到我编辑的一个单独的文件中,你为什么不接受我的答案呢?那不是很好吗?还有问题吗?