金达新到角。是否可以用所包含的模板的内容替换ng-include节点?例如,使用:
- <div ng-app>
- <script type="text/ng-template" id="test.html">
- <p>Test</p>
- </script>
- <div ng-include src="'test.html'"></div>
- </div>
生成的html是:
- <div ng-app>
- <script type="text/ng-template" id="test.html">
- <p>Test</p>
- </script>
- <div ng-include src="'test.html'">
- <span class="ng-scope"> </span>
- <p>Test</p>
- <span class="ng-scope"> </span>
- </div>
- </div>
但我想要的是:
- <div ng-app>
- <script type="text/ng-template" id="test.html">
- <p>Test</p>
- </script>
- <p>Test</p>
- </div>
我有同样的问题,仍然希望ng-include的功能包括动态模板。我正在构建一个动态的Bootstrap工具栏,我需要更干净的标记的CSS样式正确应用。
这里是我想出的对于感兴趣的人的解决方案:
HTML:
- <div ng-include src="dynamicTemplatePath" include-replace></div>
自定义指令:
- app.directive('includeReplace',function () {
- return {
- require: 'ngInclude',restrict: 'A',/* optional */
- link: function (scope,el,attrs) {
- el.replaceWith(el.children());
- }
- };
- });
如果在上面的示例中使用了此解决方案,将scope.dynamicTemplatePath设置为“test.html”将导致所需的标记。