简体   繁体   English

Angular.JS - 是否可以在指令中拥有多个视图?

[英]Angular.JS - Is it possible to have multiple views in a directive?

I'm trying to find out if you can have more than one html file in a directive and then depending on what you pass through the html tag it shows a different HTML layout. 我试图找出你是否可以在一个指令中有多个html文件,然后根据你通过html标签传递的内容,它会显示不同的HTML布局。

This is something that is required for a accordion that I am building. 这是我正在建造的手风琴所需要的东西。 We want to be able to re-use the directive but sometimes the html will be of a different layout. 我们希望能够重复使用该指令,但有时html将采用不同的布局。

So far I believe this can be done by the following: 到目前为止,我相信这可以通过以下方式完成:

<accordion data-attr="layout('feedback')></accordion>

Then in the Js check what has been passed and point the accodion to a different view. 然后在Js中检查已经通过的内容并将该指针指向另一个视图。

The bit I'm unsure about is pointing the accordion directive to a different html file. 我不确定的是将accordion指令指向另一个html文件。

You could simply use the ng-include directive in your template: 您只需在模板中使用ng-include指令:

module.directive('accordion', function() {
  return {
    scope: { type: '&' },
    template: '<div ng-include="type()"></div>'
  };
});

Yes, in your directive you can set your template to be a function that accepts the element and attributes. 是的,在您的指令中,您可以将模板设置为接受元素和属性的函数。

.directive('accordion', function() {
  return {
    templateUrl: function(elem, attr){
      return 'layout-' + attr.type + '.html';
    }
  };
});

And use it like: 并使用它像:

<accordion type="some-file"></accordion>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM