0

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??

1 Answer 1

2

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.
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.