我在 Wordpress 中使用 AngularJS,使用短代码将 HTML 插入到帖子正文中。
问题是我只能在帖子内容的正文中添加我的应用程序和 Controller 属性:
<div id="content">
<div class="entry-content">
<div class="dynamic-content" ng-app="dynamicContent" ng-controller="dynamicContent as p">
</div>
</div>
</div>
<div id="sidebar">
</div>
假设我希望我的应用程序也能控制边栏中的一个区域。我不得不修改一些核心文件,以便在 DOM 中获得更高的 ng-app
和 ng-controller
。
这很麻烦,因为将 Angular 应用程序轻松插入帖子(无需修改更多页面代码)是一种特权。
我已经尝试使用 jQuery 将 ng-app
和 ng-controller
属性添加到页面 body
中,但这当然不行'工作。
除了延迟 Angular,使用 jQuery 插入这些属性,然后在属性就位后让 Angular 运行之外,还有什么方法可以让我的 Controller “扩展”到 DOM 树的其他部分?
最佳答案
试试 manual application bootstrap method ,它允许在不使用 ng-app
的情况下将 Angular 应用程序绑定(bind)到 DOM:
angular.element(document).ready(function() {
angular.bootstrap(document, ['myApp']);
});
关于javascript - AngularJS:DOM 中的 "Expand" Controller (当您无法修改整个页面 HTML 时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36020787/