Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular ng-repeat. Looping through the letters in string

I'va just started learning Angular and can't manage an issue with ng-repeat.

I want to display every letter from input in a single span element. I tried many ways but nothing works.

Here's my code on codepen

like image 856
Arkej Avatar asked Dec 22 '25 01:12

Arkej


1 Answers

Is this what you're looking for ?

Each letter will be printed in different span.

solution of Error: ngRepeat:dupes

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
  
});
.center-block {
  float: none;
}

input {
  margin: 20px;
}

span:hover {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl" class="container">

  <div class="col-sm-10 center-block text-center">

    <input type="text" class="form-control" placeholder="Type to search..." ng-model="word" />

    <div ng-repeat="letter in word.split('') track by $index">
      <span> {{ letter }} </span>
    </div>
    <br/><br/> If you want to split the word by line <br/><br/>
    <div ng-repeat="letter in word.split(' ') track by $index">
      <span> {{ letter }} </span>
    </div>
  </div>
</div>
like image 166
Mohit Tanwani Avatar answered Dec 23 '25 14:12

Mohit Tanwani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!