Guy*_*Guy 9 javascript asp.net asp.net-mvc angularjs angularjs-bootstrap
我有一个带有ASP.NET应用程序的iframe,它包含UpdatePanel.我开始在应用程序中使用Angular,但是因为.NET回发而无法正常工作.
要解决这个问题,我使用了这个解决方案
with (Sys.WebForms.PageRequestManager.getInstance()) {
add_endRequest(onEndRequest); // regester to the end Request
}
function onEndRequest(sender, args) {
angular.bootstrap($('#mainDiv'), ['defaultApp']);
var rootscope = angular.element('#mainDiv').scope();
if (rootscope) {
rootscope.$apply();
}
}
Run Code Online (Sandbox Code Playgroud)
而且效果很好.
问题是当我在ASP.NET页面中使用另一个ng-controller动态加载不同的用户控件时,Angular会抛出一个错误,说明该应用已经加载:
App Already Bootstrapped with this Element
Run Code Online (Sandbox Code Playgroud)
所以问题是:我如何检查应用程序是否已经自举?我可以重装这个模块吗?我可以从元素中删除它而不是再次引导它吗?
谢谢.
Ada*_*ick 16
从应用程序外部访问作用域并不是一个好习惯,因此在精心构建的生产应用程序中不能启用它.如果您需要访问/应用范围,那么您的用例会有一些奇怪/不受支持的内容.
但是,检查元素是否已经自举的正确方法是Angular库执行此操作的方式,即加载元素并检查注入器.所以你想要angular.element(document.querySelector('#mainDiv')).injector();哪个代码:
function onEndRequest(sender, args) {
var element = angular.element(document.querySelector('#mainDiv'));
//This will be truthy if initialized and falsey otherwise.
var isInitialized = element.injector();
if (!isInitialized) {
angular.bootstrap(element, ['defaultApp']);
}
// Can't get at scope, and you shouldn't be doing so anyway
}
Run Code Online (Sandbox Code Playgroud)
你能告诉我们为什么你需要申请范围吗?
您可以简单地检查 的范围mainDiv,如果angular.element(document.querySelector('#mainDiv')).scope()不是undefined则表示angular尚未初始化。
您的代码将如下所示。
代码
function onEndRequest(sender, args) {
//below flag will be undefined if app has not bootsrap by angular.
var doesAppInitialized = angular.element(document.querySelector('#mainDiv')).scope();
if (angular.isUndefined(doesAppInitialized)) //if it is not
angular.bootstrap($('#mainDiv'), ['defaultApp']);
var rootscope = angular.element('#mainDiv').scope();
if (rootscope) {
rootscope.$apply(); //I don't know why you are applying a scope.this may cause an issue
}
}
Run Code Online (Sandbox Code Playgroud)
更新
在 2015 年 8 月下旬发布 Angular 1.3+ 之后,它通过禁用调试信息来添加与性能相关的改进。因此,通常我们应该将 debuginfo 选项设置为 false,以便在生产环境中获得良好的性能提升。我不想写太多关于它的内容,因为 @AdamMcCormick 的答案已经涵盖了它,这真的很酷。
| 归档时间: |
|
| 查看次数: |
7297 次 |
| 最近记录: |