2015-11-06 29 views
2

嗨动态模板,我想创建角模板动态象下面这样:创建angularjs

的Index.html:

<div ui-view></div> 
    <ul class="nav-links"> 
     <li ng-repeat="item in items"> 
      <a ui-sref="{{item.id}}"">{{item.name}}</a> 
     </li>      
    </ul> 

    <script ng-repeat="item in items" type="text/ng-template" id="{{item.id}}.html"> 
    <h1>{{item.name}}</h1> 
    </script> 

我怎么能这样做呢?我在网上搜索,发现我们可以动态地包含模板,但是我没有找到指定如何动态创建模板本身的地方。

感谢, Sombir

+0

使用指令。 –

+0

我想实现Breadcrumbs的url方案,是否可以使用指令? – CuriousGeek

+3

当然可以。有人做了http://ncuillery.github.io/angular-breadcrumb还没有 –

回答

2

你能做到这一点的最好办法是自定义指令!这是什么让angularjs强大。

.directive('myCustomer', function() { 
    return { 
    restrict: 'E', 
    template: 'myCustomer.html' 
    }; 
}); 

,并在您的索引或任何你可以叫它:

<my-customer></my-customer> 

编辑: 要生成dynamicaly您的自定义指令使用属性控制器或链接。

.directive('myCustomer', function() { 
    return { 
    restrict: 'E', 
    template: 'myCustomer.html', 
    controller: function ($scope,$http) { 
    // here you can use $scope, $http etcs... but 
    }, // to make it clean and standard use Link 
    link: function (scope, element, attrs) { 
     // do all stuff like API call JQuery stuff and all here 
    } 
    }; 
}); 

与您可以生成dynamicaly您从API或者任何你想做的指令:)。 :)

+0

即使我使用指令,我也必须使用模板,并且我想要动态创建模板。 – CuriousGeek