我正在尝试基于this article在AngularJS中创建一个工厂。但我得到一个错误说" dataFactory"在我的控制器中未定义。我的代码如下所示:
MyApp.factory('dataFactory', ['$http', function($http)
{
var urlBase='/someurl/roles/';
var dataFactory={};
dataFactory.getRoles=function()
{
return $http.get(urlBase+'getlist');
};
dataFactory.addRole=function(roleName, roleCss)
{
return $http.get(urlBase+'add&name='+roleName+'&css='+roleCss);
};
dataFactory.updateRole=function(role)
{
return $http.get(urlBase+edit&id='+role.id+'&name='+role.name+'&css='+role.css');
};
dataFactory.deleteRole=function(role)
{
return $http.get(urlBase+'remove&id='+role.id);
};
return dataFactory;
}]);
MyApp.controller('RoleCtrl',['$scope','$http','dataFactory', function($scope,$http,$rootScope,dataFactory) {
dataFactory.getRoles().then(function(res){
$scope.roles = res.data.result;
});
}]);
我做错了什么?
答案 0 :(得分:27)
你错过了'$ rootScope'的字符串版本
所以而不是
MyApp.controller('RoleCtrl',['$scope','$http','dataFactory', function($scope,$http,$rootScope,dataFactory) {
使用(注意添加'$rootScope'
):
MyApp.controller('RoleCtrl',['$scope','$http','$rootScope','dataFactory', function($scope,$http,$rootScope,dataFactory) {
由于你的参数不对齐, dataFactory
进入$ rootscope。然后没有什么可以发送到dataFactory
。因此你的错误。