指令不插值,在模板字符串(directive not interpolating, in a tem

2019-07-21 21:38发布

我试图有条件地建立一个模板。 我买了一些div和跨越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
            };
        });

Answer 1:

我不认为将角插东西放到一个指令名。 {{}}秒(自动)成立了一个$手表。 当$手表注意到一个变化,它会更新视图,但它不会调用$编译,这是我觉得需要在这里发生。

所以,我会尝试生成指令的链接功能的HTML /模板,然后$编译。 就像是:

scope.$watch('pluginui', function(newValue) {
   var jqLiteWrappedElement = angular.element('<div ' + newValue + ' width=...');
   element.replaceWith(jqLiteWrappedElement);
   $compile(jqLiteWrappedElement)(scope);
})

记住注入$compile成指令。



文章来源: directive not interpolating, in a template string