条件付きでテンプレートを作成しようとしています。いくつかのdivとspanを持つk2pluginディレクティブを取得しました。pluginui属性に従って、テンプレートの最後に別のディレクティブを挿入したいと思います。次の私のコードは、pluginui以外のすべてを補間します。たとえば、最後のdivは次のようになります。
<div {{pluginui}} width='300' height='420'></div>
{{pluginui}}はリテラルですが、他のディレクティブをトリガーするために補間する必要があります。面白いことに、同じ行の別の場所(たとえば、タグの間)に{{pluginui}}を配置すると、補間されます。
何が間違っているのですか?
app.directive("k2plugin", function () {
return {
restrict: "A",
scope: true,
link: function (scope, elements, attrs) {
console.log ("creating plugin");
// this won't work immediatley. attribute pluginname will be undefined as soon as this is called.
scope.pluginname = "Loading...";
scope.pluginid = null;
// observe changes to interpolated attributes
// [...] observe name, id, width, height
attrs.$observe('pluginui', function(value) {
console.log('pluginui has changed value to ' + value);
scope.pluginui = attrs.pluginui + "viewport";
});
},
template: "<div>\
<div>\
<span>{{pluginname}}</span>\
<span ng-click=\"resize(pluginid)\">_</span> <span ng-click=\"remove(pluginid)\">X</span>\
</div>\
<div {{pluginui}} width='{{pluginwidth}}' height='{{pluginheight}}'></div>\
</div>",
replace: true,
};
});
app.directive("canvasviewport", function () {
return {
restrict: "A",
scope: true,
link: function (scope, elements, attrs) {
console.log ("creating canvas viewport");
},
template: "<canvas width='{{pluginwidth}}' height='{{pluginheight}}'></canvas>",
replace: true
};
});
app.directive("divviewport", function () {
return {
restrict: "A",
scope: true,
link: function (scope, elements, attrs) {
console.log ("creating div viewport");
},
template: "<div style=\"width='{{pluginwidth}}' height='{{pluginheight}}'\"></div>",
replace: true
};
});
app.directive("autoviewport", function () {
return {
restrict: "A",
scope: true,
link: function (scope, elements, attrs) {
console.log ("creating auto viewport");
},
template: "<canvas width='{{pluginwidth}}' height='{{pluginheight}}'></canvas>",
replace: true
};
});