javascript - 在指令中使用链接函数附加不同​​的 HTML 元素

标签 javascript html angularjs angularjs-directive

Fiddle

我有两个按钮。当按下时,它会显示一个带有一些文本的模式。但我还想根据按下的按钮动态添加一些 html。

我已经尝试了 $observe$watch 方法,但在使其工作时遇到问题。

这是我的代码。

angular.module('TM', [])

.controller('protocolCtrl', function(){
    this.text = 'Now looking at the protocol part';
    this.modalId = 'protocolModal';
})

.controller('categoryCtrl', function(){
    this.text = 'Now looking at the category part';
    this.modalId = "categoryModal";
})

.directive('modalDirective', function(){
    return {
        restrict: 'E',
        scope: {
            ctrl: '=',
            modalId: '@',
        },
        template:  ['<div id="{{modalId}}" class="modal fade" role="dialog">', 
                      '<div class="modal-dialog">',
                        '<div class="modal-content">',
                        '<div class="modal-header">',
                        '<h4 class="modal-title">Modal Header</h4>',
                        '</div>',
                        '<div class="modal-body">',
                        '<p> {{ ctrl.text }} </p>',
                        '</div>',
                        '<div class="modal-footer">',
                        '<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>',
                        '</div>',
                        '</div>', 
                        '</div>',
                        '</div>'].join(''),
        link: function(scope, element, attrs) {
            element.$observe('modalId', function(){
                var modal = element.find('#{{modalId}}');
                if(modal == 'protocolModal'){
                    element.find('#{{modalId}}').append('<div>this is a protocol test...</div>');
                } else {
                    element.find('#{{modalId}}').append('<div>this is a category test...</div>');
                }
            });
        }
    }
});

最佳答案

我认为没有 element.$observe - 有 attrs.$observescope.$watch。您已经在范围上有了 modelId,所以让我们使用它。

此外,不要通过 id 来进行奇怪的 .find 操作,而是注入(inject)一个元素作为模板的占位符,并相应地 replaceWith :

template: '<div id="{{modalId}}">\
             ...\
             <div class="modal-body">\
               <template-placeholder></template-placeholder>\
             </div>\
           </div>",
link: function(scope, element){
  // ...

  var unwatch = scope.$watch("modalId", function(val){
    var placeholder = element.find('template-placeholder');
    if(val == 'protocolModal'){
       placeholder.replaceWith('<div>this is a protocol test...</div>');
    } else {
      placeholder.replaceWith('<div>this is a category test...</div>');
    }

    unwatch(); // seems like you don't really need to set it again
  }
}

关于javascript - 在指令中使用链接函数附加不同​​的 HTML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31532924/

相关文章:

html - 在 CSS 中缩小页面?

javascript - Html/javascript 验证失败

html - wkhtmltopdf 缺少 SVG 路径(渲染)

html - 如何使用 Angular 复选框更改 div 类

javascript - Angular 选择 ng-selected 不起作用(使用 <option ng-repeat>)

javascript - TypeScript:使用扩展运算符设置 Object.prototype

javascript - 为什么 eval 在这里失败了?

python项目中的javascript依赖

javascript - 无法以 Angular 获取在 ngOnInit() 中动态生成的数组的长度

javascript - ui-grid,滚动时出错