欢迎来到代码驿站!

AngularJS

当前位置:首页 > 网页前端 > AngularJS

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

时间:2021-01-13 09:59:10|栏目:AngularJS|点击:

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出现$http异步后台无法获取请求参数问题的解决方法

栏    目:AngularJS

下一篇:AngularJS中的$watch(),$digest()和$apply()区分

本文标题:AngularJS动态生成div的ID源码解析

本文地址:http://www.codeinn.net/misctech/44752.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有