1

New to angular. Please excuse me if this is a very simple question.

Is there anyway to convert array to string. My controller her the code:

$scope.addRow = function addRow(){     
    $scope.filters.push({ 'Name':$scope.name, 'dept': $scope.dept, 'city':$scope.city});
    $scope.name='';
    $scope.dept='';
    $scope.city='';
};

Now, I want to sent this data to a service as a string

I want it in the format "name,dept,city;name,dept,city". Is there any way to do it?

Service code looks like this:

                $scope.submit = function() {
                   myService.submit({
                      filters :$scope.filters
                 }, function(response) {
                    $scope.response = response; 
                });     
            };

I want to pass the value to filters in format "name,dept,city;name,dept,city" from the array. Thanks in advance.

4
  • no need to do it, javascript sends data as Json when you do ajax calls Commented Feb 10, 2015 at 15:34
  • it sends it as unidentified. and I want to send it in the format "name,dept,city;name,dept,city" Commented Feb 10, 2015 at 15:36
  • So, you're asking how to do a string concatenation in javascript? var myString = stringA + "," + stringB; Commented Feb 10, 2015 at 15:38
  • Editied question again. added more details Commented Feb 10, 2015 at 15:41

1 Answer 1

6

You can use

angular.toJson(array);

to create a json String of your array.

Edit:

var array = [{name: 'misko', dept: '007', city: 'new york' }, {name: 'misko', dept: '007',city: 'new york'}];

function createStringByArray(array) {
    var output = '';
    angular.forEach(array, function (object) {
        angular.forEach(object, function (value, key) {
            output += key + ',';
            output += value + ',';
        });
    });
    return output;
}

var string = createStringFromArray(array);

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

Comments

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.