AngularJS动态生成div的ID源码解析

编程学习 2021-07-04 20:00www.dzhlxh.cn编程入门
这篇文章主要介绍了基于AngularJS动态生成div的ID,本文介绍的非常详细,具有参考借鉴价值,对angularjs动态生成div的id相关知识感兴趣的朋友一起学习吧

1、问题背景

给定一个数组对象,里面是div的id;循环生成div元素,并给id赋值

2、实现源码

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>AngularJS动态生成div的ID</title> 
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> 
<script> 
var app = angular.module("idApp",[]); 
app.controller("idCon",function($scope){ 
$scope.divIds = [ 
{divId:"chartId1"}, 
{divId:"chartId2"}, 
{divId:"chartId3"}, 
{divId:"chartId4"}, 
{divId:"chartId5"} 
]; 
}); 
</script> 
</head> 
<body ng-app="idApp" ng-controller="idCon"> 
<div ng-repeat="chart in divIds"> 
<div id="{{chart.divId}}">{{chart.divId}}</div> 
</div> 
</body> 
</html>

3、实现结果

<html>

<head>

<style type="text/css">@charset "UTF-8";[ng\:cloak],[ng-cloak],[data-ng-cloak],[x-ng-cloak],.ng-cloak,.x-ng-cloak,.ng-hide:not(.ng-hide-animate){display:none !important;}ng\:form{display:block;}.ng-animate-shim{visibility:hidden;}.ng-anchor{position:absolute;}</style> 
<meta charset="UTF-8"> 
<title>angularJS动态生成div的ID</title> 
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> 
<script> 
var app = angular.module("idApp",[]); 
app.controller("idCon",function($scope){ 
$scope.divIds = [ 
{divId:"chartId1"}, 
{divId:"chartId2"}, 
{divId:"chartId3"}, 
{divId:"chartId4"}, 
{divId:"chartId5"} 
]; 
}); 
</script> 
</head> 
<body ng-controller="idCon" ng-app="idApp" class="ng-scope"> 
<!-- ngRepeat: chart in divIds --><div ng-repeat="chart in divIds" class="ng-scope"> 
<div id="chartId1" class="ng-binding">chartId1</div> 
</div><!-- end ngRepeat: chart in divIds --><div ng-repeat="chart in divIds" class="ng-scope"> 
<div id="chartId2" class="ng-binding">chartId2</div> 
</div><!-- end ngRepeat: chart in divIds --><div ng-repeat="chart in divIds" class="ng-scope"> 
<div id="chartId3" class="ng-binding">chartId3</div> 
</div><!-- end ngRepeat: chart in divIds --><div ng-repeat="chart in divIds" class="ng-scope"> 
<div id="chartId4" class="ng-binding">chartId4</div> 
</div><!-- end ngRepeat: chart in divIds --><div ng-repeat="chart in divIds" class="ng-scope"> 
<div id="chartId5" class="ng-binding">chartId5</div> 
</div><!-- end ngRepeat: chart in divIds --> 
</body>
</html>

以上所述是长沙网络推广给大家介绍的AngularJS动态生成div的ID源码解析,希望对大家有所帮助,如果大家有任何疑问请给我留言,长沙网络推广会及时回复大家的。在此也非常感谢大家对狼蚁SEO网站的支持!

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板