动态templateUrl基于属性值

时间:2014-02-18 16:04:00

标签: angularjs angularjs-directive

假设我有这些数据代表由不同面板组成的页面......

{
  "panels": [
    {
      "id": "panel1",
      "type: "carousel",
      // more panel data
    },
    { 
      "id": "panel2",
      "type": "quote",
      // data
    }]}

......我正在尝试这样做:

// template
<section ng-repeat="panel in panels">
  <my-panel panel-data="{{ panel }}"></div>
</section>

// directive
app.directive('myPanel', function() {
  return {
    scope: {
      panel: '=panelData'
    },
    templateUrl: function() {
       // panel.type ('carousel' or 'quote') determines which template to load
    }
  });

我的templateUrl功能应该是什么?

1 个答案:

答案 0 :(得分:0)

将逻辑移到template

// directive
app.directive('myPanel', function() {
  return {
    scope: {
      panel: '=panelData'
    },
    template: '<div ng-switch="panel.type">' +
                  '<div ng-switch-when="carousel" ng-include="carousel.html"></div>' +
                  '<div ng-switch-when="quote"    ng-include="quote.html"></div>' +
              '</div>'
  });