2

.filter('privateip',function() {
    return function(p) {
        p.split(',');
        return p
    }
})
<tr ng-repeat="ins in  instances">
  <td ><input type="checkbox" ng-model="icheckedList[ins.id]" ng-required="{{needOneList}}" ng-checked="all" one-least></td>
  <td><span ng-bind="ins.name"></span></td>
  <td><table><tr ng-repeat="p in ins.private_ip|privateip"></tr></table></td>
  <td ng-bind="ins.public_ip"></td>
</tr>

ins.private_ip is a string (like '192.168.1.1,192,168.1.2,192.168.1.8') which i want to transform to array, how can i do that use the filter or any way you recommend.

2 Answers 2

1

You need to return split result:

.filter('privateip',function() {
    return function(p) {
        return p.split(',');
    }
});

Strings a immutable, which means that p.split(',') doesn't mutate p, but rather returns new value (array in this case).

Sign up to request clarification or add additional context in comments.

2 Comments

It doesn't work... And I want to ask that in ng-repeat="p in ins.private_ip | privateip" the filter's parameter is private_ip? I am a newbie ...
It should work. Here is a demo: plnkr.co/edit/yaq4qJ2Zj7WzzH7D7ikE?p=preview. And privateip filter doesn't take any parameters.
0

you can ngRepeat over a function which return an array:

<td><table><tr ng-repeat="p in splitIps(ins.private_ip)"></tr></table></td>

//controller
$scope.splitIps = function(ip){
    return ip.split('.');
}

3 Comments

I had try it but when the ip='192.168.1.1' the result that ip was splited to [ ' ',' ' ] , it is so strange... but in chrome's console the split result is nomal ,how can i do to solve the problem?
its hard to tell... make sure the raw string doesn't contain any whitespaces or line breaks. you could use trim() to remove whitespaces before split it
OH ,, it's my reason , I am so sorry I forget to change something ,thank you ~~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.