Search code examples
angularjsangularjs-directiveangularjs-scopeangularjs-ng-repeat

How to create unique id's when using angular repeat with a html file as repeat


public int Order { get; set; }
        public string Name { get; set; }
        public string NameCode { get; set; }
        public string NextSectionCode { get; set; }
        public IEnumerable<string> AddText { get; set; }
        public IEnumerable<string> Languages { get; set; }
        public string TemplateUrl { get; set; }
        public string SummaryTemplateUrl { get; set; }
        public string TypeCode { get; set; }
        public int Min { get; set; }
        public int Max { get; set; }
        public string AddendumName { get; set; }
        public string AddendumDescription { get; set; }
        public bool DisplayOnly { get; set; }
        public ValidationConfiguration Validation { get; set; }
        public IDictionary<string, SectionConfiguration> Subsections { get; set; }- My service class

 agreementApiSvc.getAllWizardSections($scope.agreement.Id).then(function (response) {
            $scope.sections = response.data;
            var i = 0;
            angular.forEach($scope.sections, function (section) {
                if (!section.DisplayOnly) {

                    section.Index = i;
                    i++;
                }
                if (section.SummaryTemplateUrl) {
                    $scope.$watch(function () { return section.IsLoaded === true; }, function () {
                        $scope.sectionsLoaded++;
                        

<!-- begin snippet: js hide: false console: true babel: false -->

<div ng-repeat="section in sections | orderBy: Order">
            <agreement-summary-section id="agreement-summary-section-{{$index}}-{{section.NameCode}}" section="section" ng-disabled="isDisabled" enabled-in-amend-mode="enabledInAmendMode && !isUnderReview" agreement-status="currentAmendmentStatus"
               check-agreement-validity="checkAgreementValidity" is-enabled-in-name-change-mode="isEnabledInNameChangeMode" company-staging-status-progress="companyStagingStatusProgress"/>
        </div>

I have used a html file with a ng-repeat from the controller with html page as a "TemplateUrl"...its all working fine but the id's in the html file are repeating, so how to overcome this problem.


Solution

  • (function(angular) {
      'use strict';
    angular.module('ngRepeat', []).controller('repeatController', function($scope) {
      $scope.friends = [
        {name:'John', age:25, gender:'boy'},
        {name:'Jessie', age:30, gender:'girl'},
        {name:'Johanna', age:28, gender:'girl'},
        {name:'Joy', age:15, gender:'girl'},
        {name:'Mary', age:28, gender:'girl'},
        {name:'Peter', age:95, gender:'boy'},
        {name:'Sebastian', age:50, gender:'boy'},
        {name:'Erika', age:27, gender:'girl'},
        {name:'Patrick', age:40, gender:'boy'},
        {name:'Samantha', age:60, gender:'girl'}
      ];
    });
    })(window.angular);
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js"></script>
    </head>
    <body ng-app="ngRepeat">
      <div ng-controller="repeatController">
      I have {{friends.length}} friends. They are:
      
      <ul class="example-animate-container">
        <li class="animate-repeat" ng-repeat="friend in friends">
          [{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.
        </li>
        <li class="animate-repeat" ng-if="results.length === 0">
          <strong>No results found...</strong>
        </li>
      </ul>
    </div>
    </body>
    </html>