javascript 如何使用angular js拆分字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25946718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:16:50  来源:igfitidea点击:

How to split string using angular js

javascriptangularjssplit

提问by Himanshu Bhuyan

I am new in angular js.

我是 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 .

我想从字符串中只得到 xxx adn yyy 或 Credit 和 Debit。我使用的是 angular js 1.2.23。

回答by Jay Shukla

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

参考 -见这里

回答by katmanco

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

您甚至可以使用 Angular Js 的默认过滤器“limitTo()”。它非常有用,但不是拆分器, https://docs.angularjs.org/api/ng/filter/limitTo

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

在您的情况下,您可以像 {{cards[0] | 一样使用它 limitTo(3) }} =xxx