我正在尝试遵循https://github.com/johnpapa/angular-styleguide#style-y075上的规则,我正在尝试注入依赖项,不幸的是,它在GetDiscussionThreadListService上失败
HTML:
<discussion-parent someParam></discussion-parent>
JS:
var xDiscussionsDirectives = angular.module('xxx.discussions.parent', ['ngResource']).directive('discussionParent', discussionParrentDirective);
function discussionParrentDirective() {
var directive = {
restrict: 'E',
scope: {
discussionType: '@docDiscussionType',
},
templateUrl: 'modules/discussions/views/ParentDiscussionTemplate.aspx',
replace: false,
controller: discussionParrentDirectiveController,
//controllerAs: 'vm',
bindToController: true
};
return directive;
}
discussionParrentDirectiveController.$inject = ['$scope', '$route', '$routeParams', '$location', '$filter', '$interval', '$modal', '$timeout', 'GetDiscussionThreadListService'];
function discussionParrentDirectiveController($scope, $element, $attrs) {
GetDiscussionThreadListService.get({
//some params
}, function (data) {
}
}
ReferenceError:未定义GetDiscussionThreadListService (xxx.discussions.parrent.js:278)
答案 0 :(得分:1)
discussionParrentDirectiveController.$inject = ['$scope', '$route', '$routeParams', '$location', '$filter', '$interval', '$modal', '$timeout', 'GetDiscussionThreadListService']
和
function discussionParrentDirectiveController($scope, $element, $attrs)
需要匹配
$inject
目的是处理诸如最小化javascript之类的问题,因此模块名称需要匹配。在您的函数中,您需要具有相同的参数。
function discussionParrentDirectiveController($scope, $route, $routeParams, $location, $filter, $interval, $modal, $timeout, GetDiscussionThreadListService)
答案 1 :(得分:1)
要解决您的问题,您需要在函数参数列表中声明此模块,如下所示:
discussionParrentDirectiveController.$inject = ['$scope', '$route', '$routeParams', '$location', '$filter', '$interval', '$modal', '$timeout', 'GetDiscussionThreadListService'];
function discussionParrentDirectiveController($scope, $route, $routeParams, $location, $filter, $interval, $modal, $timeout, GetDiscussionThreadListService) {
GetDiscussionThreadListService.get({
//some params
}, function (data) {
}
这样您的变量GetDiscussionThreadListService
将在您的范围内定义。
一种替代方法是首先导入以避免所有那些无用的模块(顺便注入)。
discussionParrentDirectiveController.$inject = ['$scope', '$route', '$routeParams', 'GetDiscussionThreadListService', '$location', '$filter', '$interval', '$modal', '$timeout'];
function discussionParrentDirectiveController($scope, $route, $routeParams, GetDiscussionThreadListService) {
GetDiscussionThreadListService.get({
//some params
}, function (data) {
}
我也使用这个样式指南它是一个伟大的,继续它因为完全认为它值得。