I Love Stackoverflow
I Love Stackoverflow

Reputation: 6868

How to display last part of string containing multiple dots inside ng repeat?

Below are my list of items :

 $scope.items = [
            {id=1,name:'code.lmn.1234.Lodashjs'},
            {id=2,name:'xyz.Suv.Angularjs'},
            {id=3,name:'www.kius.reactjs'}
           ]

Now I want to display last part of name so expected output is like below :

Lodashjs
Angularjs
reactjs

I don't want to create filter for this as that will impact performance. So is there any way to do this without filter?

var app = angular.module("myApp", []);
        app.controller("myController", function ($scope) {
        $scope.items = [
            {id:1,name:'code.lmn.1234.Lodashjs'},
            {id:2,name:'xyz.Suv.Angularjs'},
            {id:3,name:'www.kius.react.js'}
           ]
        
           
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController">
   <div ng-repeat="item in items">
   <span >{{item.name}}</span>
</div>
</div>

Upvotes: -1

Views: 75

Answers (3)

Deepak Patidar
Deepak Patidar

Reputation: 394

You can also use directive that will manipulate the name and return string after last "." dot.

 <div ng-repeat="item in items" my-directive my-directive-fn="item.name">


    app.directive("myDirective", function(){
      return {
       scope: {
         "myDirectiveFn": "="
       },

       link: function(scope){
         scope.myDirectiveFn.match(/([^.]+)$/);

       }
      }
    });

Upvotes: 1

Faly
Faly

Reputation: 13356

Try this:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController">
   <div ng-repeat="item in items">
   <span >{{ item.name.split('.').reverse()[0] }}</span>
</div>
</div>

Upvotes: 2

Surjeet Bhadauriya
Surjeet Bhadauriya

Reputation: 7156

Yes it's so simple.

Just copy paste the below code:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController">
   <div ng-repeat="item in items">
   <span >{{(item.name).substr((item.name).lastIndexOf('.') + 1, (item.name).length)}}</span>
</div>
</div>

Upvotes: 1

Related Questions