Trying to replace spaces with dashes using ng-model

后端 未结 3 736
攒了一身酷
攒了一身酷 2021-01-11 13:53

I\'m new to AngularJS and trying to create a simple app that will allow me to upload files to my Laravel driven website. I want the form to show me the preview of what the u

3条回答
  •  一生所求
    2021-01-11 14:23

    There are few things missing in your filter. First of all you need to return new string. Secondary, regular expression is not correct, you should use global modifier in order to replace all space characters. Finally you also need to check if the string is defined, because initially model value can be undefined, so .replace on undefined will throw error.

    All together:

    app.filter('spaceless',function() {
        return function(input) {
            if (input) {
                return input.replace(/\s+/g, '-');    
            }
        }
    });
    

    Demo: http://plnkr.co/edit/5Rd1SLjvNI18MDpSEP0a?p=preview

提交回复
热议问题