如何拆分与angularJS字符串 [英] How to split a string with angularJS
问题描述
我想知道如果我能在angularJS简单拆分的字符串。
我有我的
I wanted to know if I can split a string simply in angularJS. I have my
$scope.test = "test1,test2";
在我的控制器,并在我看来,
我想要做这样的事情。
in my controller and in my view, I wanted to do something like that
{{test[0] | split(',')}}
{{test[1] | split(',')}}
我看到有关输入和NG-变化调用在分割字符串或者一些与NG-名单,但没有我的情况下工作的一个功能了很多东西。
I've seen a lot thing about input and ng-change calling a function in the controller that split the string or something with ng-list but nothing works in my case.
THX所有。
推荐答案
您可能想换行功能成一个过滤器,这样你就不必把mySplit功能所有的控制器。例如:
You may want to wrap that functionality up into a filter, this way you don't have to put the mySplit function in all of your controllers. For example
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];
}
});
在这里,您可以使用过滤器,你原本打算
From here, you can use a filter as you originally intended
{{test | split:',':0}}
{{test | split:',':0}}
在 http://docs.angularjs.org/guide/filter 更多信息(感谢罗斯)
Plunkr @ http://plnkr.co/edit/NA4UeL
Plunkr @ http://plnkr.co/edit/NA4UeL
这篇关于如何拆分与angularJS字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!