[英]Adding a function to the scope of an Angular custom directive
我有以下角度指令。
// The component panel
app.directive('component', function () {
return {
restrict: 'AE',
scope: {
heading: '@heading',
componentType: '@componentType'
getComponentUrl: function() {
var componentPath = "./shell/partials/components/" + componentType + ".html";
return componentPath;
}
},
template: '\
<div id="panel" class="panel panel-primary panel-component fill scrollable">\
<div class="panel-heading">\
<div class="row">\
<div class="col-sm-6">\
<h2 class="panel-title">{{heading}}</h2>\
</div>\
<div class="col-sm-6">\
<button type="button" class="btn btn-default pull-right btn-expand" data-toggle="tooltip" data-placement="left" title="Go fullscreen" ng-click="">\
<i class="fa fa-expand"></i></button>\
</div>\
</div>\
</div>\
<div class="panel-body fill">\
<!-- include is used here as different component types may have different attributes\
so includes are placed here and the directives used within the includes.\
-->\
<div ng-include="getComponentUrl()"> </div>\
</div>\
</div>\
',
link: function(scope, elm, attrs) {
}
}
});
如您所見,我在指令的范圍內使用了以下內容:
getComponentUrl: function() { ... }
這似乎不起作用。
我正在嘗試為ng-include提供預定義的字符串。 我很欣賞還有其他方法可以做到這一點,但我想知道Angular是否可以將方法放入指令范圍內?
非常感謝,基蘭
嘗試
app.directive('component', function () {
return {
restrict: 'AE',
link : function(scope){
scope.getComponentUrl = function() {
var componentPath = "./shell/partials/components/" + componentType + ".html";
return componentPath;
}
},
scope: {
heading: '@heading',
componentType: '@componentType'
},
您必須將getComponentUrl:function(){...}移至鏈接指令方法。 將此方法添加到$ scope屬性。 並在模板中引用此功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.