Reputation: 3068
I have a javascript function as follows:
if (',' in mpttexclusion){
$scope.exclusion = $scope.mpttexclusion.split(',');
} else {
$scope.exclusion = $scope.mpttexclusion;
}
But when i check the console, i am getting this error:
TypeError: Cannot use 'in' operator to search for ',' in 175
at new ModalInstanceCtrl (http://127.0.0.1:8000/static/js/app.js:180:15)
at invoke (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:3704:17)
at Object.instantiate (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:3715:23)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:6766:28
at http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.min.js:8:22381
at wrappedCallback (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:10943:81)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:11029:26
at Scope.$eval (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:11949:28)
at Scope.$digest (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:11775:31)
at Scope.$apply (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:12055:24) angular.js:9413
(anonymous function) angular.js:9413
(anonymous function) angular.js:6832
wrappedCallback angular.js:10946
(anonymous function) angular.js:11029
Scope.$eval angular.js:11949
Scope.$digest angular.js:11775
Scope.$apply angular.js:12055
(anonymous function) angular.js:17833
(anonymous function) angular.js:2612
forEach angular.js:309
eventHandler angular.js:2611
I checked a few questions already but i am not sure if i am able to totally understand the error here.
Upvotes: 0
Views: 109
Reputation: 82241
You can use indexOf instead:
if($scope.mpttexclusion.indexOf(',') >= 0)
$scope.exclusion = $scope.mpttexclusion.split(',');
else
$scope.exclusion = $scope.mpttexclusion;
Upvotes: 3
Reputation: 7404
As Frédéric pointed out in the comments of the initial question:
for (var x in object) {
...some code...
}
is rather used for Objects than for Strings.
Or as MDN describes it:
Iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.
As the others already pointed out the correct answer I just thought I link to a resource that might help you to understand the problem with the approach to being with.
Hope this helps
Upvotes: 0
Reputation: 67207
You should do like,
if (mpttexclusion.indexOf(',') > -1){
instead of
if (',' in mpttexclusion){
since in
basically used in iteration like,
var x = [10,2,3,1];
for(val in x){
console.log(val);
}
Upvotes: 1