我有一个 Grails 应用程序,它引入了几个 Grails 插件。这些插件需要被其他几个 Grails 应用程序重用,而不仅仅是我自己的。
在其中一个 Grails 插件的资源包中,我定义了以下内容:
为了完整性而保留它,但我已经切换到 Asset-Pipline
modules = {
directivea {
resource url: 'directives/directivea/directivea.js'
resource url: 'services/restapis.js'
}
}
在其中一个插件的 Javascript 文件中,我定义了以下指令:
ModuleA.directive('directivea',function() {
return {
restrict: 'EA',
scope: {
'objId' : '='
},
replace: true,
link: function(){
$scope.ObjId = attributes[''];
$scope.someFunction($scope.ObjId);
},
controller: function(){
$scope.someFunction = function(objId){
//some stuff happens here
};
},
templateUrl: 'directives/directivea/directivea.html'
}
});
它似乎可以很好地执行 Controller ,但是当它试图拉入模板时,它会窒息:
GET /appname/directives/directivea/directivea.html 404 (Not Found)
这是有道理的,因为那不是部分模板所在的位置。但它会在哪里?如何将这些信息隔离在插件中,但可供所有下游应用程序使用?如果可以的话,我想避免定义内联模板。
**编辑以包含结构**
插件结构:
引用我的插件的应用程序的结构:
**编辑以包括结构后 Assets 插件切换**
不再定义资源,BuildConfig.groovy 现在引用 Assets 管道而不是资源,web-app 中不再有文件
最佳答案
使用 Grails 资源插件
引用这个 sample grails app它使用 this grails plugin .
插件托管一个 Angular 模块。我试图通过仅添加指令而不添加其他模块来保持简单。另请注意,我使用了 static/..
在 templateUrl in 指令中。
另请参阅 Config.groovy
在使用以下配置的应用程序中
grails.resources.adhoc.includes = ['/images/**', '/css/**', '/js/**', '/img/**']
如 answer to this question 中所述.可能是您遇到了这个问题。
试试看。 “Hello World”是从插件内的 Angular 度模块中存在的指令呈现的。
如果您使用的是最新版本的 Grails,那么我建议您在方便时切换到 Assets 管道。
使用 Grails Assets 插件
使用 Grails asset-pipeline plugin (截至本文发布时为 1.9.7)和 Grails Angular-template-asset-pipeline plugin (截至本文发布时为 1.2.5)。
有关如何设置的详细信息,请参阅 How to reference a static HTML resource using the Grails Asset Pipeline plugin 的答案。 .
首先,确保您的 Angular JS 模块是小写的或在名称中使用 _ 或 - 并且您的模板文件的文件名(忽略扩展名)与您包含的任何 javascript Assets 略有不同。
关于angularjs - 如何从 Grails 插件提供的 JS 文件中指向 AngularJS 模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25251891/