TypeError: i.match is not a function

笑着哭i 提交于 2019-12-05 20:08:00
ajaykumar

I too faced this problem earlier. With further investigation I could understand that as per angular guidelines, the isolated scope can only have the attributes of the custom directives. It throws error if we try to create a new scope value which doesn't matches with any of the attribute.

myModule.directive('directiveName', function factory() {
  return {
    ...
    scope: {
      'attrName': '@', // OK
      'attrName2': '=localName', // OK
      'attrName3': '<?localName', // OK
      'attrName4': ' = name', // OK
      'attrName5': 'name',    // ERROR: missing mode @&=
      'attrName6': 'name=',   // ERROR: must be prefixed with @&=
      'attrName7': '=name?',  // ERROR: ? must come directly after the mode
    }
    ...
  }
});

The scope: $scope doesn't look right to me. I just worked through a similar nondescript angular issue where I got "TypeError: a.match is not a function" by doing

    app.directive('contactAssociation', function () {
    return {
        templateUrl: 'contactAssociation.html',
        restrict: 'E',
        scope: {
            contact:"=",
            isSelected : false
        },
        controller: contactAssociationController
    };

By merely changing the scope assignment in the directive definition to this:

    app.directive('contactAssociation', function () {
    return {
        templateUrl: 'contactAssociation.html',
        restrict: 'E',
        scope: {
            contact:"=",
        },
        controller: contactAssociationController
    };

Everything started working again. It seems that angular throws some weird errors by doing improper scope assignment and doesn't adequately check for issues.

The following are the valid scope options for a directive definition.

scope: true //inherit parent scope

scope: false //use parent scope

scope: {} //isolated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!