'indexOf'未定义?

时间:2014-09-23 13:03:57

标签: angularjs error-handling indexof

我总是得到那个错误。所以我很确定,我的" $ scope.permissionExists = function()"

有问题

有什么想法吗?如果我错过了发布相关代码 - 请告诉我

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

这是我的代码: 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" <是数组中的值

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

2 个答案:

答案 0 :(得分:2)

在if语句的$scope.permissionExists = function ()中,您有data.permissions,但这应该是$scope.data.permissions。它应该如下所示: -

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

我希望这会有所帮助。

答案 1 :(得分:2)

您需要在if条件中添加$ scope,如:

if ($scope.data.permissions.indexOf("sign_change_global") > -1){
  ...
}

由于