这是我个人对于 angular*模块*设计的思路,然后遇到一些问题,当然如果我的思路不理想,然后期待您给我建议,谢谢。
我把projects
打包成一个模块:
// 创建我的应用下面的`project`模块
var projectModule = angular.module('myApp.project', ['myApp.config'])
// 路由
projectModule.config(function ($routeProvider) {
var routes = {
'/projects': {
templateUrl: 'components/projects/list.html',
controller: 'ProjectListCtrl',
resolve: {
projects: function (projectService) {
return projectService.query().$promise
}
}
},
'/projects/:id': {
templateUrl: 'components/projects/show.html',
controller: 'ProjectCtrl',
resolve: {
// ...
}
}
}
angular.forEach(routes, function (route, path) {
$routeProvider.when(path, route)
})
})
// 服务
projectModule.factory('projectService', function ($resource) {
var url = config.API + '/projects/:id'
return $resource(url, { id: '@_id' })
})
// 项目列表
projectModule
.controller('ProjectListCtrl', function (projects) {
this.projects = projects
})
.directive('projectListDirective', function () { // `project list`指令, 因为我把它关联到`ProjectListCtrl`成为一个组件单元
return {
replace: true,
transclude: true,
scope: {
projects: '@'
},
template: '<div projects="{{ctrl.projects}}"><h1>项目列表</h1><div ng-transclude></div></div>',
controller: 'ProjectListCtrl',
controllerAs: 'ctrl',
link: function (scope, el, attrs, ctrl) {
console.log(scope)
}
}
})
// 项目详情
projectModule
.controller('ProjectCtrl', function (project) {
this.project = project
})
.directive('projectDiretive', function () {
return {
replace: true,
template: '<h1>{{ctrl.project.title}}</h1><p>{{ctrl.project.description}}</p>',
controller: 'ProjectCtrl',
controllerAs: 'ctrl',
link: function (scope, el, attrs, ctrl) {
console.log(scope)
}
}
})
components/projects/list.html
<project-list-directive>
<ul>
<li ng-repeat="project in $parent.projects">
<a ng-href="/projects/{{project.id}}">{{project.title}}</a>
</li>
</ul>
</project-list-directive>
components/projects/show.html
<project-directive></project-directive>
不过,基于我以上的思路,在 console 里面看到报错,实际上ProjectListCtrl
被 call 了两次,一次是渲染components/projects/list.html
模板的时候,然后在编译<project-list-directive>
指令的时候又调用了一次。而,这个错误就是在编译指令的时候调用ProjectListCtrl
,然后找不到porjects
这个注入依赖。
Error: [$injector:unpr] Unknown provider: projectsProvider <- projects <- ProjectListCtrl
http://errors.angularjs.org/1.3.15/$injector/unpr?p0=projectsProvider%20%3C-%20projects%20%3C-%20ProjectListCtrl
at REGEX_STRING_REGEXP (http://localhost:3000/bower_components/angular/angular.js:63:12)
at http://localhost:3000/bower_components/angular/angular.js:4015:19
at Object.getService [as get] (http://localhost:3000/bower_components/angular/angular.js:4162:39)
at http://localhost:3000/bower_components/angular/angular.js:4020:45
at getService (http://localhost:3000/bower_components/angular/angular.js:4162:39)
at Object.invoke (http://localhost:3000/bower_components/angular/angular.js:4194:13)
at $get.extend.instance (http://localhost:3000/bower_components/angular/angular.js:8493:21)
at http://localhost:3000/bower_components/angular/angular.js:7739:13
at forEach (http://localhost:3000/bower_components/angular/angular.js:331:20)
at nodeLinkFn (http://localhost:3000/bower_components/angular/angular.js:7738:11) <div ng-view="" class="ng-scope">