Comma Separated

angular

前端 未结 7 1055
清歌不尽
清歌不尽 2020-12-31 17:25

I have a pretty basic scenario (somewhat new to angular). I am trying to convert JSON:

[
    {\'name\': \'Nick\'},
    {\'name\': \'David\'},
    {\'name\':         


        
7条回答
  •  离开以前
    2020-12-31 17:59

    Here's another approach that's more maintainable using Filters:

    app.filter('commaSeparated', function () {
        return function(input, property) {
            var csv = '';
            for (var i = 0; i < input.length; i++) {
                csv += input[i][property];
                if (i < input.length - 1) {
                    csv += ',';
                }
            }
            return csv;
        };
    });
    

    Obviously, you can replace the literal array with your $scope.menuItems...

    {{ [ {'name': 'Nick'}, {'name': 'David'}, {'name': 'John'} ] | commaSeparated:'name' }}

提交回复
热议问题