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

时间:2013-05-23 13:55:59

标签: javascript angularjs angularjs-directive

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

1 个答案:

答案 0 :(得分:12)

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

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