javascript - Angular create element if another is non existant -


i have list of states (florida, alabama ...) , want create named anchors above first occurance of first letter.

letter links

  <nav>     <ul class="letters">       <li ng-repeat="letter in locations.getletters()">         <a href="#{{letter}}">{{letter}}</a>       </li>     </ul>   </nav> 

states

     <nav>         <ul class="locations">           <li ng-repeat="state in locations.states">{{state.state}}             <a ng-if="" id="{{state.state.charat(0)}}">fgsdf</a>             <ul>               <li ng-repeat="city in state.cities">                 <a href>{{city.name}}</a>               </li>             </ul>           </li>         </ul>       </nav> 

i stuck @ <a ng-if="" id="{{state.state.charat(0)}}">fgsdf</a>

i have tried ng-if="!document.getelementbyid(state.state.charat(0))" , doesn't work. have suggestions how should go this?

update

i have considered using angulars built in filters filter states on ngrepeat directive. when user clicks a, states starting show. seems cleaner , more intuitive approach , improve ux.

you can try approach

let's assume have input simple array of strings. before placing in controller, can group states first letter of each state using simple object (the letter key, value array of strings)

http://jsfiddle.net/q2y93ym7/1/

html:

<body ng-app="helloapp" ng-controller="controller" class="container">      <div class="well">         <a href="#{{letter}}" ng-repeat="(letter,states) in letters">{{letter}} </a>      </div>      <div ng-attr-id="{{letter}}" ng-repeat="(letter,states) in letters">          <h1>{{letter}}</h1>          <h2 ng-repeat="state in states">{{state}}</h2>         <hr>     </div> </body> 

js:

angular.module('helloapp', [])     .controller('controller', function ($scope) {     var states = ['alabama', 'alaska', 'arizona', 'arkansas', 'california', 'colorado', 'connecticut', 'delaware', 'florida', 'georgia', 'hawaii', 'idaho', 'illinois indiana', 'iowa', 'kansas', 'kentucky', 'louisiana', 'maine', 'maryland', 'massachusetts', 'michigan', 'minnesota', 'mississippi', 'missouri', 'montana nebraska', 'nevada', 'new hampshire', 'new jersey', 'new mexico', 'new york', 'north carolina', 'north dakota', 'ohio', 'oklahoma', 'oregon', 'pennsylvania rhode island', 'south carolina', 'south dakota', 'tennessee', 'texas', 'utah', 'vermont', 'virginia', 'washington', 'west virginia', 'wisconsin', 'wyomin'];      // let's prepare input      var letters = $scope.letters = {};     states.foreach(function (state) {         var letter = state.charat(0);         if (!letters[letter]) {             letters[letter] = [];         }          letters[letter].push(state);     });  }) 

edit:

  • as @drobinson says, nothing guarantees keys of object sorted. therefore can try using this great approach / instead object, use array
  • added <h1>{{letter}}</h1>, @tony

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

javascript - How to insert selected radio button value into table cell -

css - Transitioning Transforms in Safari look terrible/shaky/stuttering -