我已经看过几篇与此类似的帖子,不过我认为我正确地实现了依赖项,但我似乎仍然得到错误。这是代码。
的IndexController:
/**
* IndexController
* @namespace task_list.layout.controllers
*/
(function () {
'use strict';
angular
.module('task_list.layout.controllers')
.controller('IndexController', IndexController);
IndexController.$inject = ['$scope', 'Auth', 'Tasks', 'Snackbar'];
/**
* @namespace IndexController
*/
function IndexController($scope, Auth, Tasks, Snackbar) {
var vm = this;
vm.isAuthenticated = Auth.isAuthenticated();
vm.tasks = [];
activate();
/**
* @name activate
* @desc Actions to be performed when this controller is instantiated
* @memberOf task_list.layout.controllers.IndexController
*/
function activate() {
Tasks.all().then(tasksSuccessFn, tasksErrorFn);
$scope.$on('task.created', function (event, task) {
vm.tasks.unshift(task);
});
$scope.$on('task.created.error', function () {
vm.tasks.shift();
});
/**
* @name tasksSuccessFn
* @desc Update tasks array on view
*/
function tasksSuccessFn(data, status, headers, config) {
vm.tasks = data.data;
}
/**
* @name tasksErrorFn
* @desc Show snackbar with error
*/
function tasksErrorFn(data, status, headers, config) {
Snackbar.error(data.error);
}
}
}
})();
任务
/**
* Tasks
* @namespace task_list.tasks.services
*/
(function () {
'use strict';
angular
.module('task_list.tasks.services')
.factory('Tasks', Tasks);
Tasks.$inject = ['$http'];
/**
* @namespace Tasks
* @returns {Factory}
*/
function Tasks($http) {
var Tasks = {
all: all,
create: create,
// edit: edit,
// delete: delete,
get: get
};
return Tasks;
////////////////////
/**
* @name all
* @desc Get all Tasks
* @returns {Promise}
* @memberOf task_list.tasks.services.Tasks
*/
function all() {
return $http.get('/api/v1/tasks/');
}
/**
* @name create
* @desc Create a new Task
* @param {string} name The name of the new Task
* @returns {Promise}
* @memberOf task_list.tasks.services.Tasks
*/
function create(name) {
return $http.post('/api/v1/tasks/', {
name: name
});
}
/**
* @name get
* @desc Get the Tasks of a given user
* @param {string} username The username to get Tasks for
* @returns {Promise}
* @memberOf task_list.tasks.services.Tasks
*/
function get(username) {
return $http.get('/api/v1/accounts/' + username + '/tasks/');
}
}
})();
Task_list.js
(function () {
'use strict';
angular
.module('task_list', [
'task_list.routes',
'task_list.auth',
'task_list.layout',
'task_list.tasks',
'task_list.config',
'task_list.utils',
]);
angular
.module('task_list.routes', ['ngRoute']);
angular
.module('task_list.config', []);
angular
.module('task_list')
.run(run);
run.$inject = ['$http'];
/**
* @name run
* @desc Update xsrf $http headers to align with Django's defaults
*/
function run($http) {
$http.defaults.xsrfHeaderName = 'X-CSRFToken';
$http.defaults.xsrfCookieName = 'csrftoken';
}
})();
我加载了模块及其子文件夹(控制器,指令,服务)。请注意,我正在缩小文件并在我的应用中加载每个模块:
(function () {
'use strict';
angular
.module('task_list.layout', [
'task_list.layout.controllers'
]);
angular
.module('task_list.layout.controllers', []);
})();
非常感谢任何帮助。谢谢!
这是控制台日志:
c98216751c41.js:2140 Error: [$injector:unpr] Unknown provider: TasksProvider <- Tasks <- IndexController
http://errors.angularjs.org/1.5.7/$injector/unpr?p0=TasksProvider%20%3C-%20Tasks%20%3C-%20IndexController
at http://127.0.0.1:9090/static/CACHE/js/c98216751c41.js:1507:8
at http://127.0.0.1:9090/static/CACHE/js/c98216751c41.js:1721:7
at Object.getService [as get] (http://127.0.0.1:9090/static/CACHE/js/c98216751c41.js:1737:122)
at http://127.0.0.1:9090/static/CACHE/js/c98216751c41.js:1721:212
at getService (http://127.0.0.1:9090/static/CACHE/js/c98216751c41.js:1737:122)
at injectionArgs (http://127.0.0.1:9090/static/CACHE/js/c98216751c41.js:1740:58)
at Object.instantiate (http://127.0.0.1:9090/static/CACHE/js/c98216751c41.js:1747:106)
at $controller (http://127.0.0.1:9090/static/CACHE/js/c98216751c41.js:2000:20)
at Object.link (http://127.0.0.1:9090/static/CACHE/js/c98216751c41.js:2681:377)
at http://127.0.0.1:9090/static/CACHE/js/c98216751c41.js:1576:296 <div class="col-xs-12 ng-view ng-scope">
tasks.module.js
(function () {
'use strict';
angular
.module('task_list.tasks', [
'task_list.tasks.controllers',
'task_list.tasks.directives',
'task_list.tasks.services',
]);
angular
.module('task_list.tasks.controllers', []);
angular
.module('task_list.tasks.directives', ['ngDialog']);
angular
.module('task_list.tasks.services', []);
})();
加载js文件的顺序:
<script type="text/javascript" src="{% static 'javascripts/task_list.js' %}"></script>
<script type="text/javascript" src="{% static 'javascripts/task_list.config.js' %}"></script>
<script type="text/javascript" src="{% static 'javascripts/task_list.routes.js' %}"></script>
<script type="text/javascript" src="{% static 'javascripts/auth/auth.module.js' %}"></script>
<script type="text/javascript" src="{% static 'javascripts/auth/services/auth.service.js' %}"></script>
<script type="text/javascript" src="{% static 'javascripts/auth/controllers/register.controller.js' %}"></script>
<script type="text/javascript" src="{% static 'javascripts/tasks/tasks.module.js' %}"></script>
<script type="text/javascript" src="{% static 'javascripts/auth/controllers/login.controller.js' %}"></script>
<script type="text/javascript" src="{% static 'javascripts/layout/layout.module.js' %}"></script>
<script type="text/javascript" src="{% static 'javascripts/layout/controllers/navbar.controller.js' %}"></script>
<script type="text/javascript" src="{% static 'javascripts/tasks/directives/tasks.directive.js' %}"></script>
<script type="text/javascript" src="{% static 'javascripts/tasks/controllers/tasks.controller.js' %}"></script>
<script type="text/javascript" src="{% static 'javascripts/utils/utils.module.js' %}"></script>
<script type="text/javascript" src="{% static 'javascripts/utils/services/snackbar.service.js' %}"></script>
<script type="text/javascript" src="{% static 'javascripts/layout/controllers/index.controller.js' %}"></script>
答案 0 :(得分:0)
我能够解决我的问题。我实际上并没有在我的html文件中加载tasks_list.service脚本。当我编辑原始评论时,我能够更好地看到它。我不知怎的错过了。感谢帮助我隔离它的回复。
我的html文件中缺少代码:
<script type="text/javascript" src="{% static 'javascripts/tasks/services/tasks.service.js' %}"></script>