Reputation: 5133
I have the following situation
directives.directive('x', function () {
return {
scope: {
a: "=",
b: "="
},
controller: function($scope){
if((angular.isDefined($scope.a) && !angular.isDefined($scope.b))
|| (!angular.isDefined($scope.a) && angular.isDefined($scope.b))){
throw new Error('a and b need to be specified');
}
}
};
}
I have a problem with my if
inside the controller. As you may see, it is kind of hard to read it and it's not very efficient.
What it is trying to do is to see if both a
and b
were defined on $scope
. They can both miss but if one of them is specified, the other one must become mandatory.
Is there a better way to write this?
Later edit: Lets also things about when case of having three variables on scope and we need the same behavior. How will you proceed?
Upvotes: 0
Views: 55
Reputation: 755
You can use the function isUndefined
if(angular.isUndefined($scope.a) || angular.isUndefined($scope.b))
{
throw new Error('a and b need to be specified');
}
Upvotes: 1