【发布时间】:2015-04-02 15:44:42
【问题描述】:
所以,使用 angularJS,我想将我的指令分隔在它们自己的文件中以更清晰。 我尝试了一些在这里和其他地方找到的建议,但没有奏效。
目前我的指令位于控制器文件中,该文件也是我定义模块的文件。
Controller.js:
var app = angular.module("viewerApp", ["ngDialog"]);
app.config(['ngDialogProvider', function (ngDialogProvider) {
ngDialogProvider.setDefaults({
className: 'ngdialog-theme-default',
plain: false,
showClose: true,
closeByDocument: true,
closeByEscape: true,
appendTo: false,
preCloseCallback: function () {
console.log('default pre-close callback');
}
});
}]);
app.controller('viewerController', function ($scope, $rootScope, ngDialog) {
/*
Here I have most of my $scope manipulations and functions
*/
});
app.controller('InsideCtrl', function ($scope, ngDialog) {
/*
Small controller to handle some dialog event
*/
});
app.directive('myDirective', function() {
return {
template: 'Directive, lang: {{ currentLanguage }}'
};
})
然后在我的 HTML 文件中:
<body ng-keypress="handleKeys($event)" ng-controller="viewerController">
{{ "Default : " + defaultLanguage + " - Current : " + currentLanguage }}<br />
<my-directive></my-directive>
所以这种方式工作正常,我的测试模板按我想要的方式显示。
但是当我将指令对应的代码移动到一个新文件时,它就停止工作了。
仅供参考,我在新文件中尝试了以下内容:
app.directive('myDirective', function() {
return {
template: 'Directive, lang: {{ currentLanguage }}'
}
});
/* ----------------- */
angular.module("viewerApp", ["ngDialog"]).directive('myDirective', function() {
return {
template: 'Directive, lang: {{ currentLanguage }}'
};
})
/* ----------------- */
angular.module("viewerApp").directive('myDirective', function() {
return {
template: 'Directive, lang: {{ currentLanguage }}'
};
})
我该如何解决这个问题?
问候。
【问题讨论】:
-
并且您已经在您的页面中使用
<script>标签包含了您的directive.js 文件? -
是的,我做到了。在我在这里咨询任何答案之前,我找到了一个作为答案发布的解决方案。但我不确定这是一种正确的方法。
标签: angularjs angularjs-directive