如何在angular js中向多个ng include指令添加ng include回调
how to add ng-include callback to multiple ng-include directives in angular js
我有这样的HTML
<form ng-controller="testCtrl1">
<div ng-include="'test/views/navigationbar.html'"></div>
<div ng-include="'test/views/processnav.html'"></div>
<div ng-include="'test/views/leftprocesstabs.html'"></div>
</div>
</form>
我想写一个通用的方法来检查我所有的ng包含文件是否已加载。
加载完所有文件后,我需要进行服务器调用。
有没有办法在angular js中检查这个?
使用每个模板的onload并递增一个计数器。如果表单只包含ng-includediv,请使用beow代码来获取计数,或者编写一个函数来获取带有ng-includ的div的计数。
HTML
<form ng-controller="testCtrl1" id="includes">
<div ng-include="'test/views/navigationbar.html'" onload="load()"></div>
<div ng-include="'test/views/processnav.html'" onload="load()"></div>
<div ng-include="'test/views/leftprocesstabs.html'" onload="load()"></div>
</div>
</form>
Javascript
var templates = 0;
var length = $("#includes > div").length;
$scope.load = function(){
templates++;
if(templates >= length){
onLoadComplete()
}
}
function onLoadComplete(){
// all templates are loaded
}
ng-include
触发一个通过模板缓存的请求。这是异步完成的,所以不,angular不能在所有模板完成加载时为您提供触发器。
您可以将模板预取到缓存中,并从中处理事件。。。
例如,
// inject $q, $http, $templateCache to your controller
...
var promises = [];
promises.push($http.get('test/views/navigationbar.html',{ cache : $templateCache }));
promises.push($http.get('test/views/processnav.html',{ cache : $templateCache }));
promises.push($http.get('test/views/leftprocesstabs.html',{ cache : $templateCache }));
$q.all(promises, function (data) {
console.log('All templates are loaded into the cache !');
});
...
相关文章:
- AngularJS ng include dons'不起作用
- ng include导致角度问题
- AngularJS ng include won'不知什么原因不起作用
- 如何将动态文件名传递给ng-include
- 有没有办法在AngularJS中强制重新加载递归ng-include
- 使用ng-include渲染Angular模板时出现问题
- 每个ng-include模板都有单独的变量
- AngularJS:如何使用ng-include将javascript文件加载到部分中
- 阻止“ng include”创建孤立的作用域.AngularJS
- 如何扩展 Angularjs ng-include 指令
- 在 ng-include 中使用变量作为表达式
- ng-class 在 angularJs 中由 ng-include 包含的文件内不起作用
- 模块化与性能与性能与角度应用程序中的ng-include
- 未捕获的错误: [$injector:modulerr] - 角度 ng-include
- 使用ng-include调用控制器时AngularJS路由解析
- angular js中嵌套ng include内的表单
- 在AngularJS中动态切换ng include和ng控制器
- 使用ng-include重用以前实例化的模板
- 页面不可滚动,其中ng include=“”;函数()"-该代码已不再使用
- 如何在双大括号表示法{{}}中使用ng-include和html文件