0

I am new in angular js.

my array -

"cards":[
"xxx Bank Credit Card",
"yyy Bank Debit Card"
],

what i am tring get only xxx adn yyy or Credit and Debit from the string .I am using angular js 1.2.23 .

Himanshu Bhuyan
  • 306
  • 2
  • 8
  • 20

2 Answers2

1

Try this

angular.module('myModule', [])
    .filter('split', function() {
        return function(input, splitChar, splitIndex) {
            // do some bounds checking here to ensure it has that index
            return input.split(splitChar)[splitIndex];
        }
    });

Use in view like

{{test | split(',',0)}}

Ref - See here

Community
  • 1
  • 1
Jay Shukla
  • 5,794
  • 8
  • 33
  • 63
  • if I have to add this split function on `ng-model` instead of {{ }} expression, then how to add filter on ng-model parameter – ojus kulkarni Apr 05 '17 at 06:12
  • You can not use filter with `ng-model`. I think you want to apply filter on your ng-model value which you can achieve with custom `$watch` on that model. – Jay Shukla Apr 05 '17 at 09:30
  • ya I can not use it on ng-model..... but my question is how to use it on ng-model value? in controller? using $filter? or can we use? – ojus kulkarni Apr 05 '17 at 09:55
0

You can even use default Filter of Angular Js " limitTo() " . it is very helpful but not a splitter , https://docs.angularjs.org/api/ng/filter/limitTo

in your case you can use it like {{cards[0] | limitTo(3) }} =xxx

katmanco
  • 1,146
  • 12
  • 24