1. Contexte du problème
Étant donné un objet tableau, qui contient l'identifiant de p; boucle pour générer p éléments et attribuer des valeurs à l'identifiant
2. Code source de mise en œuvre
<!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>
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>
Ce qui précède est le contenu d'AngularJS générant dynamiquement l'ID de p. vers le site PHP chinois (m.sbmmt.com) !
Articles associés :
AngularJS implémente la méthode de compilation dynamique et d'ajout à domAngularJs charge dynamiquement les modules et les dépendancesAngularJS implémente la méthode de liaison d'événements à des éléments générés dynamiquement