AngularJS动态生成div的ID

黄舟
Lepaskan: 2017-03-25 16:12:04
asal
1616 orang telah melayarinya

1、问题背景

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

2、实现源码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>AngularJS动态生成p的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.pIds = [
					{pId:"chartId1"},
					{pId:"chartId2"},
					{pId:"chartId3"},
					{pId:"chartId4"},
					{pId:"chartId5"}
				];
			});
		</script>
	</head>
	<body ng-app="idApp" ng-controller="idCon">
		<p ng-repeat="chart in pIds">
			<p id="{{chart.pId}}">{{chart.pId}}</p>
		</p>
	</body>
</html>
Salin selepas log masuk

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动态生成p的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.pIds = [
					{pId:"chartId1"},
					{pId:"chartId2"},
					{pId:"chartId3"},
					{pId:"chartId4"},
					{pId:"chartId5"}
				];
			});
		</script>
	</head>
	<body ng-controller="idCon" ng-app="idApp" class="ng-scope">
		<!-- ngRepeat: chart in pIds --><p ng-repeat="chart in pIds" class="ng-scope">
			<p id="chartId1" class="ng-binding">chartId1</p>
		</p><!-- end ngRepeat: chart in pIds --><p ng-repeat="chart in pIds" class="ng-scope">
			<p id="chartId2" class="ng-binding">chartId2</p>
		</p><!-- end ngRepeat: chart in pIds --><p ng-repeat="chart in pIds" class="ng-scope">
			<p id="chartId3" class="ng-binding">chartId3</p>
		</p><!-- end ngRepeat: chart in pIds --><p ng-repeat="chart in pIds" class="ng-scope">
			<p id="chartId4" class="ng-binding">chartId4</p>
		</p><!-- end ngRepeat: chart in pIds --><p ng-repeat="chart in pIds" class="ng-scope">
			<p id="chartId5" class="ng-binding">chartId5</p>
		</p><!-- end ngRepeat: chart in pIds -->
	

</body></html>
Salin selepas log masuk

1180.png

 以上就是AngularJS动态生成p的ID的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!

相关文章:

AngularJS实现动态编译添加到dom中的方法

AngularJs 动态加载模块和依赖

AngularJS实现给动态生成的元素绑定事件的方法

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan