Create url from an array in angularjs

2

I have the following array:

$scope.arreglo = [
        {"clave_acceso":"122"},
      {"clave_acceso":"222"},
      {"clave_acceso":"333"}
    ];

and from the desire to create a url which should be like the following: If the fix has a data: http: services / authorizations? Ca = 122 If the fix has more than one data: http: services / authorizations? Ca = 122 & ca = 222 & ca = 33 ....... etc

in my controller I have the following:

$scope.url ="";
    $scope.arreglo = [
        {"clave_acceso":"122"},
      {"clave_acceso":"222"},
      {"clave_acceso":"333"}
    ];

        for(var i =0; i < $scope.arreglo.length;i++){
        if($scope.arreglo.length >= 2){
        $scope.url="http:services/autorizaciones?ca="+$scope.arreglo[i].clave_acceso+"&ca="+$scope.arreglo[i].clave_acceso;
        }else{
            $scope.url="http:services/autorizaciones?ca="+$scope.arreglo[i].clave_acceso;
        }
      }

and at the end of everything I print in my html view the url

<h5>{{url}}</h5>

But something I'm doing wrong that does not appear as I expect.

How can I solve it? I thank you in advance

    
asked by Dimoreno 31.01.2017 в 15:50
source

1 answer

1

We will simply concatenate the clave_acceso to a string url = "http:services/autorizaciones";

var arreglo = [{
  "clave_acceso": "122"
}, {
  "clave_acceso": "222"
}, {
  "clave_acceso": "333"
}];
url = "http:services/autorizaciones";
for (var i = 0; i < arreglo.length; i++) {
  url += "&ca=" + arreglo[i].clave_acceso;
}
url = url.replace("&", "?");
console.log(url)

when the for ends the result is :

http:services/autorizaciones&ca=122&ca=222&ca=333

and with the replace function we eliminate the first & and replace it with ?

variable url use it to show in the view

    
answered by 31.01.2017 / 16:04
source