前端在做数据渲染的时候经常会遇到在数据渲染完毕后执行某些操作,这几天就一直遇到在列表和表格渲染完毕后,执行点击和选择操作。对于angularjs处理这类问题,最好的方式就是指令 directive。
首先,定义指令:
app.directive(\' renderfilters\', function ($timeout) {
return {
restrict: \'A\',
: function (scope, element, attr) {
if (scope.$last === true) { //判断是否是最后一条数据
$timeout(function () {
scope.$emit(\'ngRepeatFinished\'); //向父级scope传送ngRepeatFinished命令
});
}
}
};
});
其次,指令定义完毕后,需要将指令添加到迭代的标签内,此处是<tr>标签
<div class=\"fixed-table-container\" style=\"margin-right: 0px;\">
<table class=\"table table-hover lamp-table\">
<thead>
<tr>
<th></th>
<th style=\"text-align: center; \" data-field=\"name_device-id\" tabindex=\"0\"
ng-repeat=\"i in provider.geoZoneListHead track by $index\" ng-hide=i.bol>
<div class=\"th-inner sortable \" style=\"padding-right: 10px\">{{i.name}}
</div>
<div class=\"fht-cell\" style=\"width: 101px;\"></div>
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat=\"i in provider.geoZoneList\" renderfilters>
<td><input data-index=\"0\" name=\"btSelectItem\" type=\"radio\"
value=\"{{$index}}\" ng-click=\"selectInput($index)\"></td>
<td class=\"nameId0\">{{$index+1}}</td>
<td class=\"nameId1\">{{i.geoZoneName}}</td>
<td class=\"nameId2\">{{i.de ion}}</td>
<td class=\"nameId3\">{{i.totalNumberOfMembers}}</td>
<td class=\"nameId4\">{{i.country}}</td>
<td class=\"nameId5\">{{i.lastUpdateDate}}</td>
</tr>
</tbody>
</table>
</div>
最后,在最后一条数据渲染完毕后,brodercast是向子级scope传送事件(命令)。而on()是监听事件,监听brodercast是否将事件(命令)传送回来,若事件已传送回来,则表示数据已经渲染完毕,就可以执行以后的其他操作了
$scope.$on(\'ngRepeatFinished\', function (ngRepeatFinishedEvent) {
var btnList = $(\"input[name=\'btSelectItem\']\");
btnList.eq(0).attr(\"checked\",\"checked\");
$scope.provider.detalOutlet();
});
在没有angularJs的时候一般通过监听 事件来确定页面是否加载完成。但在使用angularJs来渲染页面时, 事件不能保证angularJs是否完成了对页面的渲染。最常见的情况就是用angularJs来加载某个数据Table时,我们得等这个Table加载完之后对Table上的数据进行操作,但因为这个Table是由AngularJs渲染的,所以得找到某个方法获得AngularJs渲染完毕后的事件。 这也就是为什么 事件在angularJs框架上数据刷新不执行的一个原因,因为angularJs是数据驱动,根据数据的更新进行页面的刷新,而整体页面已经加载完成(数据更新,angularJs数据渲染,页面不会重新加载),故 事件判定页面没有变化,所以不予执行!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
下一篇 :
AI可以使用活动跟踪应用程序预测您的生命周期
-
<丰田发布了LQ EV概念车>。丰田全新的概念车配备了AI代理和自动驾驶功能,这是丰田美国公司研究员开发的,首次的公开亮相将在本月23日。在2017年CES消费车展上丰田曾展示了 Concept-Ai i概念车
2026-05-19栏目: 教程
-
Sysweld笔记:利用稳态算法加速算法模拟焊接过程的残余应力
2026-05-19栏目: 教程
-
Jvm-Sandbox源码分析--启动时加载模块
2026-05-19栏目: 教程
-
免费的分布式事务来了——阿里巴巴Fescar
2026-05-19栏目: 教程
-
升级iOS 13后真能随意换字体?库克言:想多了,字体管理≠换字体
2026-05-19栏目: 教程
