【发布时间】:2014-07-16 19:28:41
【问题描述】:
我有一个非常大的 index.html 文件,因此我将其拆分为不同的部分 html 文件,我想通过指令将这些文件加载到索引 shell 文件中。下面是 index.html 的相关部分:
<div north-view></div>
<div west-view></div>
<div center-view></div>
<div east-view></div>
然后我有另一个名为 directives.js 的文件,它将 templateUrls 分配给这些文件:
'use strict';
/* Directives */
angular.module('cstarsApp').
directive('westView', function() {
return {
templateUrl: 'partials/west-view.html'
};
}).
directive('centerView', function() {
return {
templateUrl: 'partials/center-view.html'
};
}).
directive('eastView', function() {
return {
templateUrl: 'partials/east-view.html'
};
}).
directive('northView', function() {
return {
templateUrl: 'partials/north-view.html'
};
});
templateUrl 位指定的部分 html 文件如下所示(例如 center-view.html):
<div class="accordion ui-layout-center" id="searchResultsAccordion" ng-controller="SearchResultsController">
SEARCH RESULTS
<h3 class="ui-accordion-header">Search Results Word Cloud</h3>
<div id="searchResultsWordCloud" style="width:200px; height:100px">
</div>
<h3>Search Results</h3>
<div class="accordion" id="searchResultsDocumentsAccordion">
<table id='searchResultsTable' class="table table-hover table-condensed table-striped" cellpadding="0" cellspacing="0" border="1" width="100%">
Some more html..
....................
</table>
</div>
</div>
问题是此设置无法正确加载部分 html 文件:看起来部分内容显示在网页上,但没有我期望的样式/javascript。
但是,如果我摆脱自定义指令,只需将部分内容粘贴到我指定的 index.html 中,它就可以完美运行。这里发生了什么事?指令的返回值中是否缺少一些参数?提前感谢您的帮助。
【问题讨论】:
-
如果逻辑不工作,它可能没有从范围中获取控制器。您需要在指令中公开它,否则它无法访问它。关于样式 - 这有点奇怪。如果您在其中一个视图加载后手动调用 $digest,它会起作用吗?
-
我认为你是对的,因为它没有从范围中获取控制器。我究竟如何“在指令中公开它”?
标签: javascript html angularjs angularjs-directive