Sanath
Sanath

Reputation: 4886

angular inside the service string argument converted to array

I have the following angular structure

//html     
<input type="text" model="name">
<div ng-if="!validateMyname(name)"></div>

//controller
 
$scope.validateMyname = function(arg){   
   console.log(arg);  //LINE 1 
   return myService.validateXY(arguments);
}

//service
angular.module('services').factory('myService', function () {
    myService.validateXY = function(a){
        console.log(a); //LINE 2
     }
     return myService;

}):

I can see the string that I have input in LINE 1. but in LINE 2, its an array. Any reason why??

Upvotes: 0

Views: 54

Answers (1)

PSL
PSL

Reputation: 123739

You are passing arguments to the service function, it is not an array it is just array like. And it is a collection of all arguments passed into the function validateMyname. If you want to just pass name you could just do:

 return myService.validateXY(arg);

So in short:

Line1 - You are just using the passing in value via the function's argument which is a local variable arg defined in the scope of that function.

Line2 - You are accessing the special argument object that was passed in from the line after LINE1

if you are looking to pass variable number of arguments transferred from controller function to your service function you could do so by using function.apply:

 myService.validateXY.apply(null, arguments); //null provided you don't refer to context via this.

Upvotes: 2

Related Questions