How do I trim() a string in angularjs?

后端 未结 5 2013
误落风尘
误落风尘 2021-02-05 08:51

Is there an angular specific way? If not, should I use the built in jquery to do it? If I should use the built in jquery how do I get to the trim() function without using $ (o

5条回答
  •  Happy的楠姐
    2021-02-05 09:07

    If you need only display the trimmed value then I'd suggest against manipulating the original string and using a filter instead.

    app.filter('trim', function () {
        return function(value) {
            if(!angular.isString(value)) {
                return value;
            }  
            return value.replace(/^\s+|\s+$/g, ''); // you could use .trim, but it's not going to work in IE<9
        };
    });
    

    And then

    {{ foo | trim }}
    

提交回复
热议问题