My angular function is defined in a service somethin like,
$delegate.isConfigurable = function (product, config) {
if (product) {
///.....
}
return config.getDetail();
};
Now, on the controller
, I am calling this through:
mySer.isConfigurable(null, conf);
But passing null
looks a little awakard to me.
I tried calling mySer.isConfigurable(conf);
but this makes conf
match to first parameter
of the function.
Instead, are there any other way of passing only conf
?
My angular function is defined in a service somethin like,
$delegate.isConfigurable = function (product, config) {
if (product) {
///.....
}
return config.getDetail();
};
Now, on the controller
, I am calling this through:
mySer.isConfigurable(null, conf);
But passing null
looks a little awakard to me.
I tried calling mySer.isConfigurable(conf);
but this makes conf
match to first parameter
of the function.
Instead, are there any other way of passing only conf
?
3 Answers
Reset to default 3Maybe you can just switch the order that the parameters are defined?
$delegate.isConfigurable = function (config, product) {
if (product) {
///.....
}
return config.getDetail();
};
Then you can call just mySer.isConfigurable(conf);
and product
be undefined in that case, and will evaluate to false.
If you have a function that have varying number of parameters then you can use the arguments object
Here is the documentation
The arguments object is an Array-like object corresponding to the arguments passed to a function.The arguments object is a local variable available within all functions
So your code will be
$delegate.isConfigurable = function() {
var param = null,
config = null;
if (arguments && arguments.length == 2) {
// you have param and config
param = arguments[0];
config = arguments[1];
} else if (arguments && arguments.length == 1) {
config = arguments[0];
}
return config.getDetail();
};
And you can call this by one or two or n number of param
mySer.isConfigurable(conf)
mySer.isConfigurable(param,conf)
mySer.isConfigurable(param1,param2,conf)
If you don't want to use null
then you need to interchange your function parameter like bellow.
$delegate.isConfigurable = function (config, product) {
if (product) {
///.....
}
return config.getDetail();
};
And now if you call that function passing one parameter that will be the value of config
and product
parameter will be passed as undefined
.
mySer.isConfigurable(conf);