Flowdo Flowdo - 3 months ago 15
AngularJS Question

'indexOf' is undefined?

I always get that error. So I am pretty sure, there is something wrong with my "$scope.permissionExists = function ()"

Any ideas? If I missed posting relevant code- please tell me so

> TypeError: Cannot read property 'indexOf' of undefined
> at Scope.$scope.permissionExists(http://0.0.0.0:9000/scripts/controllers/signature.js:64:31)
> at Object.get (http://0.0.0.0:9000/bower_components/angular/angular.js:11064:21)
> at Scope.$digest (http://0.0.0.0:9000/bower_components/angular/angular.js:12775:40)
> at Scope.$apply (http://0.0.0.0:9000/bower_components/angular/angular.js:13050:24)
> at done (http://0.0.0.0:9000/bower_components/angular/angular.js:8525:45)
> at completeRequest (http://0.0.0.0:9000/bower_components/angular/angular.js:8739:7)
> at XMLHttpRequest.xhr.onreadystatechange (http://0.0.0.0:9000/bower_components/angular/angular.js:8678:11)


here is my code: JS

$scope.data = {
newCustomerSig: null,
newAccountSig: null,
user: null,
account: null,
permissions: null,
maxSms: 1
};
....
var getAccountPermission = function () {
// check only when authenticated (or refresh possible)
AccountPermissions.one().getList().then(
function (resultOk) {
var permission = resultOk.data;
$log.d("AccountPermissions ok: ", resultOk);
$scope.data.permissions = permission;
},
function (resultError) {
$log.d("Accountinfo error: ", resultError);
ErrorService.showApiError(resultError);
},
function (resultNotify) {
$log.d("Accountinfo notify:", resultNotify);
}
);
};

$scope.permissionExists = function () {
if (data.permissions.indexOf("sign_change_global") > -1) {
//In the array!
return true;
} else {
return false;
//Not in the array
}
};
....


"sign_change_global" < is a value in the array

HTML

....
<form name="formSignature" class="">
<div>
<i> Persönliche Signatur </i><br/>
<textarea id="persSignatur" name="persSignatur"
rows="5" cols="40"
use-sms-counter="data.smsCounter"
ng-model="data.newAccountSig"
ng-trim="false"
placeholder="{{data.user.signature}}">
</textarea>
<div>
{{data.newAccountSignewAccountSig}}
{{data.smsCounter.textLength}}
{{data.smsCounter.isUcs2}}
<div class="amount"
data-sms-counter="data.smsCounter"
data-sms-counter-content-list="[data.newAccountSig]"
data-max-messages="1">
</div>
{{data.smsCounter}}

</div>

</div>
....

Answer

In your $scope.permissionExists = function () in the if statement you have got data.permissions however this should be $scope.data.permissions. It should look like this: -

$scope.permissionExists = function () {
      if ($scope.data.permissions.indexOf("sign_change_global") > -1) {

I hope this helps.