带空格的AngularJS自定义指令参数

AngularJS Custom Directive Argument with Spaces

本文关键字:指令 参数 自定义 AngularJS 空格      更新时间:2023-09-26

我有一个自定义指令,用于标准化日期输入并格式化它们以匹配我的(有点奇怪的)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'
    }
  }
}]);

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

scope: {
      'dateLabel': '@dateLabel'
}