6

我有一个自定义指令用于标准化日期输入并格式化它们以匹配我的(有点奇怪)API 要求。用于调用它的标签如下:

<date-input date-id="birthDate" date-label="Date Of Birth" ng-model="client.dateOfBirth"></date-input>

我收到以下错误:

Syntax Error: Token 'Of' is an unexpected token at column 6 of the expression [Date Of Birth] starting at [Of Birth].

当我删除空格时(即date-label="DateOfBirth"它工作正常。)

如何在指令属性中允许空格?

指令:

directives.directive('dateInput', [function() {
  var link = function(scope, element, attrs, model) {
    scope.dateLabel = attrs.dateLabel;
    scope.dateId = attrs.dateId;

    var dateObjectPre = moment(scope.dateObject);
    scope.dateObjectPre = dateObjectPre.format('MMDDYYYY');

    scope.update = function() {
      var dateObject;
      if(angular.isDefined(scope.dateObjectPre)) {
        dateObject = moment(scope.dateObjectPre, 'MMDDYYYY');
      }

      if (dateObject && dateObject.isValid()) {
        scope.dateObject = dateObject.format('YYYY-MM-DD');
      }
      else {
        scope.dateObject = '';
      }
    };
  };

  return {
    restrict: 'E',
    link: link,
    templateUrl: '/views/directives/dateInput.html',
    replace: true,
    scope: {
      'dateLabel': '=dateLabel',
      'dateObject': '=ngModel',
      'dateShow': '=dateShow',
      'dateRequired': '=dateRequired',
      'dateId': '=dateId'
    }
  }
}]);
4

1 回答 1

12

将属性值传递给指令时,您不应该使用 @ 吗?

scope: {
      'dateLabel': '@dateLabel'
}
于 2013-05-23T14:03:34.050 回答