【发布时间】:2016-07-14 20:51:58
【问题描述】:
我正在尝试创建一个自定义角度组件,该组件可以根据 templateUrl 函数动态加载模板。我目前得到一个 templateUrl 没有使用显式注释并且不能在严格模式下调用的错误。通常我知道当注入的服务没有得到正确注释时会出现这个错误(https://docs.angularjs.org/error/$injector/strictdi)。但是,我错过了这如何适用于templateUrl。
我正在使用 Angular 1.5。
确切的错误信息是 -
angular.js:13550 Error: [$injector:strictdi] templateUrl is not using explicit annotation and cannot be invoked in strict mode
组件代码sn-p:
angular.module('hive.triGrid')
.controller('TriGridCellController', ['$element', '$attrs', function ($element, $attrs) {
var $ctrl = this;
}])
.component('triGridCell', {
controller: 'TriGridCellController',
templateUrl: function($element, $attrs)
{
var type = $attrs.cellType;
if(type.toUpperCase() == "ICON")
{
return "components/grid/cellTemplates/iconCell.tpl.html";
}
else if(type.toUpperCase() == "CUSTOM")
{
return $attrs.cellTemplateUrl;
}
else
{
return "components/grid/cellTemplates/textCell.tpl.html";
}
},
//template:"<ng-include src='$ctrl.getTemplateUrl(z)'/>",
bindings: {
cellData:'<',
cellType: '<', //See triGridRow and triGrid for config JSON format.
}
});
编辑: 应用答案后的代码:
templateUrl: ['$element', '$attrs', function($element, $attrs)
{
var type = $attrs.cellType;
if(type.toUpperCase() == "ICON")
{
return "components/grid/cellTemplates/iconCell.tpl.html";
}
else if(type.toUpperCase() == "CUSTOM")
{
return $attrs.cellTemplateUrl;
}
else
{
return "components/grid/cellTemplates/textCell.tpl.html";
}
}],
【问题讨论】:
标签: javascript angularjs